Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring MVC将URL映射到控制器的问题_Java_Spring_Web Applications_Spring Mvc - Fatal编程技术网

Java Spring MVC将URL映射到控制器的问题

Java Spring MVC将URL映射到控制器的问题,java,spring,web-applications,spring-mvc,Java,Spring,Web Applications,Spring Mvc,我正在使用SpringMVC开发一个web应用程序 我有两个控制器:QuestionController和TodoController——它们都扩展了MultiActionController,并且都定义了list()方法 我在my web.xml中为dispatcher定义了映射,如下所示: <servlet-mapping> <servlet-name>app</servlet-name> <url-pattern>/todo/

我正在使用SpringMVC开发一个web应用程序

我有两个控制器:QuestionController和TodoController——它们都扩展了MultiActionController,并且都定义了
list()
方法

我在my web.xml中为dispatcher定义了映射,如下所示:

<servlet-mapping>
    <servlet-name>app</servlet-name>
    <url-pattern>/todo/*</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>app</servlet-name>
    <url-pattern>/question/*</url-pattern>
</servlet-mapping>  
<bean id="handlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">        
    <property name="mappings">
        <props>
            <prop key="/todo/**">todoController</prop>
            <prop key="/question/**">questionController</prop>
        </props>
    </property>
2011-08-08 15:23:38,098 [http-8080-3] DEBUG org.springframework.web.servlet.DispatcherServlet - DispatcherServlet with name 'app' determining Last-Modified value for [/app/todo/list]
2011-08-08 15:23:38,098 [http-8080-3] DEBUG org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping - Mapping [/list] to handler 'com.tmm.enterprise.controller.QuestionController@1ef4b'
2011-08-08 15:23:38,133 [http-8080-3] DEBUG org.springframework.web.servlet.DispatcherServlet - Last-Modified value for [/app/todo/list] is: -1
2011-08-08 15:23:38,133 [http-8080-3] DEBUG org.springframework.web.servlet.DispatcherServlet - DispatcherServlet with name 'app' processing GET request for [/app/todo/list]
2011-08-08 15:23:38,133 [http-8080-3] DEBUG org.springframework.web.bind.annotation.support.HandlerMethodInvoker - Invoking request handler method: public org.springframework.web.servlet.ModelAndView com.tmm.enterprise.controller.QuestionController.list(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) throws java.lang.Exception
正如您所看到的-出于某种原因,尽管URL与TodoController的映射相匹配,但现在仍选择了问题控制器

有什么想法吗


更新

这两个控制器的定义如下:

<servlet-mapping>
    <servlet-name>app</servlet-name>
    <url-pattern>/todo/*</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>app</servlet-name>
    <url-pattern>/question/*</url-pattern>
</servlet-mapping>  
<bean id="handlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">        
    <property name="mappings">
        <props>
            <prop key="/todo/**">todoController</prop>
            <prop key="/question/**">questionController</prop>
        </props>
    </property>
2011-08-08 15:23:38,098 [http-8080-3] DEBUG org.springframework.web.servlet.DispatcherServlet - DispatcherServlet with name 'app' determining Last-Modified value for [/app/todo/list]
2011-08-08 15:23:38,098 [http-8080-3] DEBUG org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping - Mapping [/list] to handler 'com.tmm.enterprise.controller.QuestionController@1ef4b'
2011-08-08 15:23:38,133 [http-8080-3] DEBUG org.springframework.web.servlet.DispatcherServlet - Last-Modified value for [/app/todo/list] is: -1
2011-08-08 15:23:38,133 [http-8080-3] DEBUG org.springframework.web.servlet.DispatcherServlet - DispatcherServlet with name 'app' processing GET request for [/app/todo/list]
2011-08-08 15:23:38,133 [http-8080-3] DEBUG org.springframework.web.bind.annotation.support.HandlerMethodInvoker - Invoking request handler method: public org.springframework.web.servlet.ModelAndView com.tmm.enterprise.controller.QuestionController.list(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) throws java.lang.Exception
QuestionController.java

@Controller
public class QuestionController extends MultiActionController implements InitializingBean 
{

    @RequestMapping("/list")
    public ModelAndView list(HttpServletRequest request, HttpServletResponse response) throws Exception{
        //Display List view for questions
    }

    @RequestMapping("/detail/{questionId}")
    public ModelAndView detail(@PathVariable("questionId") long questionId, HttpServletRequest request, HttpServletResponse response) throws Exception{
        //Display Detailed Question View for questionId...
    }
}
public class TodoController extends MultiActionController implements InitializingBean 
{

    @Transactional
    public ModelAndView list(HttpServletRequest request, HttpServletResponse response) throws Exception{
        //Display list of Todos
    }

}
ToDoController.java

@Controller
public class QuestionController extends MultiActionController implements InitializingBean 
{

    @RequestMapping("/list")
    public ModelAndView list(HttpServletRequest request, HttpServletResponse response) throws Exception{
        //Display List view for questions
    }

    @RequestMapping("/detail/{questionId}")
    public ModelAndView detail(@PathVariable("questionId") long questionId, HttpServletRequest request, HttpServletResponse response) throws Exception{
        //Display Detailed Question View for questionId...
    }
}
public class TodoController extends MultiActionController implements InitializingBean 
{

    @Transactional
    public ModelAndView list(HttpServletRequest request, HttpServletResponse response) throws Exception{
        //Display list of Todos
    }

}

如您所见,我只在QuestionController上添加了注释,而不是ToDo控制器。我也没有在注释中再次明确定义到问题控制器的URL映射(我知道我可以将
@RequestMapping(“/Question”)
在类级别-但由于这已经在配置中定义,我没有在这里添加它。此外,如果我在这里也定义了它,那么我会得到一个
ResourceNotFound
错误,但是如果我导航到
/question/question/list
,那么它会正确加载)

为什么使用旧配置,而不是注释?两个控制器的控制器代码是什么样子的?@Bozho,这只是个人偏好-我更喜欢有一个中央配置来定义我的控制器类和关联的URL映射,因为我个人觉得它更容易获取,而不必通过类来查找所有控制器及其映射。@nicholas.hauschild-更新了控制器的原始问题。@BozhoSpringSource工具套件Eclipse插件添加了一个名为RequestMappings的视图,该视图在一个位置显示所有映射,当然,它们是通过注释定义的。