我在配置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
}
}
没有进一步的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>
这一次,当我导航到/ question / list时,它仍然无法加载正确的页面,但是日志显示Spring至少识别出正确的Controller,但却没有找到方法(在它没有找到之前)根据URL找到Controller):
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'
在我看来,这是一个相对简单的事情,我试图通过使用注释将Controller连接到URL,但它无法正常工作 - 有任何人遇到这个或看到任何明显的错误我的角色?
更新
我的调查取得了一些进展。
在我的web.xml中,我按如下方式定义servlet映射:
<servlet-mapping>
<servlet-name>microblog</servlet-name>
<url-pattern>/question/*</url-pattern>
</servlet-mapping>
如果我删除了这个servlet映射,(我仍然有一个将所有.html
映射到同一个servlet的servlet映射)并将我正在使用的URL更改为/question/list.html
然后它可以工作(也是我将问题控制器中我的@RequestMapping
方法上的list()
注释的方法级别映射更改为/list.html
)。
总结:
我有servlet映射/question
到Web上下文
我有/question
到QuestionController
我的/list
现在我不希望我的网址以这些情况结束.html - 有谁知道如何解决这个问题?似乎servlet映射可能会从URL中删除匹配的/question
字符串(因此/question/list
无效,但/question/question/list
正在工作)
答案 0 :(得分:4)
设置
<url-pattern>/</url-pattern>
没有html的网址 并使用
<mvc:resources mapping="/resources/**" location="/resources/" />
用于.css,.jpg等资源
答案 1 :(得分:2)
您的控制器映射中不需要/ **。
映射到@RequestMapping(value = "/question")
会转到此控制器。
/list
将附加到/question
。
当你添加/**
就像你拥有它一样,你告诉它寻找问题的基本路径,然后是任何东西,然后将/list
添加到最后。
希望有所帮助。
答案 2 :(得分:0)
我也遇到了这个问题,但是没有开发只调用Rest API然后错误:&#34; org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException:没有为servlet请求找到匹配的处理程序方法..... &#34;
我通过添加&#34;接受:application / json&#34;来解决此问题。进入Rest客户端工具中的Header。希望它可以帮到你。