Spring MVC - 使用注释将控制器映射到URL

rhi*_*nds 5 java spring web-applications spring-mvc

我一直有问题配置我的Spring Controller映射到特定的URL,我把它归结为一个相当简单的场景,我认为应该工作:

我正在使用注释配置我的Controller类,它看起来如下:

@Controller
@RequestMapping(value = "/question/**")
public class QuestionController 
{

    /**
     * Method to build users questions list
     * 
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping("/list")
    public ModelAndView list(HttpServletRequest request, HttpServletResponse response) throws Exception{
        //Display Questions List
    }
}
Run Code Online (Sandbox Code Playgroud)

Controller没有进一步的配置,我只需要在webmvc配置中进行<mvc:annotation-driven/>配置和<context:component-scan>..配置,这样就可以自动检测控制器.

现在,当我导航到/question/list应用程序无法找到资源时,我收到ResourceNotFound错误.但是,如果我导航到/question/question/list那么应用程序加载我期望正确的页面.

任何想法为什么这是指导方法使用/question/question/list


在此之后我尝试将配置添加到我的webmvc配置以强制所有人RequestMappings使用参数alwaysUseFullPath = true,我这样做如下:

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" p:order="3">
        <property name="alwaysUseFullPath" value="true" />
    </bean>
Run Code Online (Sandbox Code Playgroud)

这一次,当我导航到/ question/list时,它仍然无法加载正确的页面,但是日志显示Spring至少识别出正确的Controller,但是却没有找到该方法(在它甚至找不到Controller之前)基于URL):

2011-08-09 18:02:27,885 [http-8080-3] DEBUG org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping - Matching patterns for request [/question/list] are [/question/**/list/, /question/**/list, /question/**/, /question/**]
2011-08-09 18:02:27,886 [http-8080-3] DEBUG org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping - Mapping [/question/list] to handler 'com.tmm.enterprise.microblog.controller.QuestionController@143c423'
2011-08-09 18:02:27,886 [http-8080-3] DEBUG org.springframework.web.servlet.DispatcherServlet - Last-Modified value for [/microblog/question/list] is: -1
2011-08-09 18:02:27,886 [http-8080-3] DEBUG org.springframework.web.servlet.DispatcherServlet - DispatcherServlet with name 'microblog' processing GET request for [/microblog/question/list]
2011-08-09 18:02:27,886 [http-8080-3] DEBUG org.springframework.web.servlet.handler.SimpleMappingExceptionResolver - Resolving exception from handler [com.tmm.enterprise.microblog.controller.QuestionController@143c423]: org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException: No matching handler method found for servlet request: path '/list', method 'GET', parameters map[[empty]]
2011-08-09 18:02:27,886 [http-8080-3] DEBUG org.springframework.web.servlet.handler.SimpleMappingExceptionResolver - Resolving to view 'resourceNotFound' for exception of type [org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException], based on exception mapping [.NoSuchRequestHandlingMethodException]
2011-08-09 18:02:27,887 [http-8080-3] DEBUG org.springframework.web.servlet.handler.SimpleMappingExceptionResolver - Exposing Exception as model attribute 'exception'
Run Code Online (Sandbox Code Playgroud)

在我看来,似乎是一个相对简单的事情,我试图通过使用注释将Controller连接到URL,但它无法正常工作 - 有没有人遇到这个或看到我的任何明显的错误?


UPDATE

我在调查方面取得了一些进展.

在我的web.xml中,我定义了servlet映射,如下所示:

<servlet-mapping>
        <servlet-name>microblog</servlet-name>
        <url-pattern>/question/*</url-pattern>
    </servlet-mapping>
Run Code Online (Sandbox Code Playgroud)

如果我删除这个servlet映射,(我还有一个servlet映射,所有映射.html到同一个servlet),并改变我使用的URL /question/list.html,然后它工作(也是我改变的方法级映射@RequestMapping在我的注释list()的方法问题控制者/list.html).

综上所述:

  1. 我有servlet映射/question到Web上下文

  2. 我有另一个映射/questionQuestionController

  3. 我有一个方法级别映射我的列表方法 /list

现在我不希望我的URL以这些情况结束.html - 有谁知道如何解决这个问题?似乎servlet映射可能会/question从URL中删除匹配的字符串(因此/question/list无法工作,但/question/question/list正在工作)

Gen*_*gzu 4

<url-pattern>/</url-pattern> 
Run Code Online (Sandbox Code Playgroud)

对于没有 html 的 url 并使用

<mvc:resources mapping="/resources/**" location="/resources/" />
Run Code Online (Sandbox Code Playgroud)

对于 .css、.jpg 等资源