2015-07-05 93 views
0

我用spring 4.1開發。我嘗試我的網址與控制器彈簧URL映射的簡單問題

這地圖是我的web.xml文件

<!-- Spring MVC --> 
    <servlet> 
    <servlet-name>mvc-dispatcher</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <load-on-startup>1</load-on-startup> 
</servlet> 
<servlet-mapping> 
    <servlet-name>mvc-dispatcher</servlet-name> 
    <url-pattern>/*</url-pattern> 
</servlet-mapping> 

這裏的提取物是我的Spring XML文件

 <bean id="viewResolver" 
     class="org.springframework.web.servlet.view.UrlBasedViewResolver"> 
     <property name="viewClass" 
      value="org.springframework.web.servlet.view.JstlView" /> 
     <property name="prefix"> 
     <value>/WEB-INF/pages/</value> 
     </property> 
     <property name="suffix"> 
     <value>.jsp</value> 
     </property> 
    </bean> 

<context:component-scan base-package="orm,orm.impl,web.controller.impl,web.view" /> 

這裏的提取物的提取物我的控制器文件

package web.controller.impl; 

import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 
import java.util.Map; 

import org.apache.logging.log4j.LogManager; 
import org.apache.logging.log4j.Logger; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.beans.factory.annotation.Qualifier; 
import org.springframework.stereotype.Controller; 
import org.springframework.ui.ModelMap; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.servlet.ModelAndView; 

import service.CommonManagementService; 
import web.view.QuestionItem; 
import domain.Question; 

@Controller 
public class ComboController { 



    private static final Logger LOGGER = LogManager.getLogger(); 

    @Autowired 
    @Qualifier("commonManagementService") 
    private CommonManagementService commonManagementService; 

    public CommonManagementService getCommonManagementService() { 
     return this.commonManagementService; 
    } 

    @RequestMapping(value = "/unsecure/getQuestion", method = RequestMethod.GET) 
    public ModelAndView getQuestion(ModelMap model) { 
     LOGGER.info("debut methode getQuestion"); 
     final List<QuestionItem> results = new ArrayList<QuestionItem>(); 
     final List<Question> questions = this.commonManagementService.getQuestions(); 
     for (final Question question : questions) { 
      results.add(new QuestionItem(question.getQuestion(), question.getId().toString())); 
     } 
     final Map<String, Object> modelToPass = new HashMap<String, Object>(); 
     model.put("items", results); 
     LOGGER.info("fin methode getQuestion"); 
     return new ModelAndView("jsonResultView", model); 

    } 

    public void setCommonManagementService(
      CommonManagementService commonManagementService) { 
     this.commonManagementService = commonManagementService; 
    } 
} 

我的應用程序的名稱是tennisArc1600,當我嘗試發送網址 http://localhost:8080/tennisArc1600/unsecure/getQuestionhttp://localhost:8080/unsecure/getQuestion 我得到的錯誤404的ressource不可

預先感謝您爲您的建議

回答

0

在web.xml中使用

<url-pattern>/</url-pattern> 

然後聲明

<mvc:annotation-driven/> 

或者

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"> 
    <property name="order" value="1" /> 
</bean> 

在Spring Web配置

0

你好測試Testini,感謝您的回答。

我成功地到達了我的控制器。這裏是我的配置

的web.xml

<!-- Spring MVC --> 
    <servlet> 
    <servlet-name>mvc-dispatcher</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value> 
    </init-param>  
    <load-on-startup>1</load-on-startup> 
</servlet> 
<servlet-mapping> 
    <servlet-name>mvc-dispatcher</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping> 

MVC-調度-servlet.xml中

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-4.1.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-4.1.xsd 
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"> 

    <!-- <context:component-scan base-package="domain,orm,orm.impl" /> --> 

    <bean 
     class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer"> 
     <property name="locations"> 
      <list> 
       <value>classpath:messages.properties</value> 
       <value>classpath:persistence.properties</value> 
       <value>classpath:securities.properties</value> 
       <value>classpath:mail.properties</value> 
       <value>classpath:views.properties</value> 
      </list> 
     </property> 
     <property name="ignoreUnresolvablePlaceholders" value="true" /> 
    </bean> 

    <bean id="viewResolver" 
     class="org.springframework.web.servlet.view.UrlBasedViewResolver"> 
     <property name="viewClass" 
      value="org.springframework.web.servlet.view.JstlView" /> 
     <property name="prefix"> 
      <value>/WEB-INF/pages/</value> 
     </property> 
     <property name="suffix"> 
      <value>.jsp</value> 
     </property> 
    </bean> 

    <bean id="abstractViewResolver" class="org.springframework.web.servlet.view.ResourceBundleViewResolver"> 
     <property name="basename" value="views"/> 
     <property name="order" value="0"/> 
    </bean> 

    <mvc:resources mapping="/resources/**" 
     location="/, classpath:/WEB-INF/public-resources/" cache-period="10000" /> 

    <mvc:annotation-driven /> 

    <context:component-scan base-package="orm,orm.impl,web.controller.impl,web.view" /> 

</beans> 

我控制器

package web.controller.impl; 

import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 
import java.util.Map; 

import org.apache.logging.log4j.LogManager; 
import org.apache.logging.log4j.Logger; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.beans.factory.annotation.Qualifier; 
import org.springframework.stereotype.Controller; 
import org.springframework.ui.ModelMap; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.servlet.ModelAndView; 

import service.CommonManagementService; 
import utils.Result; 
import web.view.QuestionItem; 
import domain.Question; 

@Controller 
public class ComboController { 



    private static final Logger LOGGER = LogManager.getLogger(); 

    @Autowired 
    @Qualifier("commonManagementService") 
    private CommonManagementService commonManagementService; 

    public CommonManagementService getCommonManagementService() { 
     return this.commonManagementService; 
    } 

    @RequestMapping(value = "/unsecure/getQuestion.htm", method = RequestMethod.GET) 
    public ModelAndView getQuestion(ModelMap model) { 
     LOGGER.info("debut methode getQuestion"); 
     final List<QuestionItem> results = new ArrayList<QuestionItem>(); 
     final List<Question> questions = this.commonManagementService.getQuestions(); 
     for (final Question question : questions) { 
      results.add(new QuestionItem(question.getQuestion(), question.getId().toString())); 
     } 
     final Map<String, Object> modelToPass = new HashMap<String, Object>(); 
     model.put("items", results); 
     model.put("results", new ArrayList<Result>()); 
     LOGGER.info("fin methode getQuestion"); 
     return new ModelAndView("jsonResultView", model); 

    } 

    public void setCommonManagementService(
      CommonManagementService commonManagementService) { 
     this.commonManagementService = commonManagementService; 
    } 
} 

再次感謝

+0

喜@flamant,一般來說,如果你是受歡迎的你認爲一個答案解決了你的問題/問題,你應該接受它作爲問題的答案。 –