【发布时间】:2011-08-09 17:06:33
【问题描述】:
我在配置 Spring 控制器以映射到特定 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'
在我看来,在使用注释将控制器连接到 URL 时,我试图实现相对简单的事情,但它无法正常工作 - 有没有人遇到过这个问题或看到任何明显的错误我的部分?
更新
我的调查取得了一些进展。
在我的 web.xml 中,我将 servlet 映射定义如下:
<servlet-mapping>
<servlet-name>microblog</servlet-name>
<url-pattern>/question/*</url-pattern>
</servlet-mapping>
如果我删除这个 servlet 映射,(我仍然有一个 servlet 映射,将所有 .html 映射到同一个 servlet)并将我正在使用的 URL 更改为 /question/list.html 然后它可以工作(我也更改了方法级别将问题控制器中我的list() 方法上的@RequestMapping 注释映射到/list.html)。
总结:
我有 servlet 映射
/question到 web 上下文我有另一个
/question到QuestionController的映射我的
/list列表方法有一个方法级别映射
现在我不希望我的 URL 在这些情况下以 .html 结尾 - 有谁知道我可以如何解决这个问题?似乎 servlet 映射可能会从 URL 中删除匹配的 /question 字符串(因此 /question/list 不起作用,但 /question/question/list 起作用)
【问题讨论】:
-
为没有 html 的 url 设置
/ -
如果我使用它,它会将我的所有图像、css 资源等重定向到控制器,因此无法找到它们(我所有的图像都在 /images/.. 等) - 所以当我加载该页面没有图像或 CSS 样式。有什么想法可以将 URL 映射到控制器而不需要复制 URL 中的路径?
-
将您的文件移动到上面的文件夹中,而不是在 WEB-INF 文件夹中。
-
抱歉,我错了 - 我的图像、css 等已经在 WEB-INF 之上的目录级别。任何想法为什么当我添加
/ 并导航到页面时没有样式/图像出现? -
哦,使用
标签: java spring web-applications spring-mvc