SpringMVC中可能存在的错误:无法处理像/或/*这样的servlet路径



我想我在springMVC中发现了一个错误,如果web xml中的servlet路径只有"/"或"/*"而没有单引号,JstlView就不起作用。您可以自己尝试,使用任何基于springMVC的工作web应用程序,然后调整dispatcher servlet路径以仅匹配"/"或"/*",然后访问页面(当然使用新的正确url),您会得到400错误。

这是我的例子,我有一个springmvc应用程序,它在web xml:中

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/mvc/*</url-pattern>
</servlet-mapping>

我部署并访问:

http://localhost:8080/contextroot/mvc/template

页面看起来非常好,下面是日志:

21:24:05.024 [http-bio-8080-exec-92] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Looking up handler method for path /template
21:24:05.048 [http-bio-8080-exec-92] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Returning handler method [public java.lang.String com.project.www.controller.ResultController.getPersonList(org.springframework.ui.ModelMap)]
21:24:05.050 [http-bio-8080-exec-92] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'resultController'
21:24:05.056 [http-bio-8080-exec-92] DEBUG o.s.web.servlet.DispatcherServlet - Last-Modified value for [/contextroot/mvc/template] is: -1
21:24:05.104 [http-bio-8080-exec-92] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Invoking afterPropertiesSet() on bean with name 'template'
21:24:05.106 [http-bio-8080-exec-92] DEBUG o.s.web.servlet.DispatcherServlet - Rendering view [org.springframework.web.servlet.view.JstlView: name 'template'; URL [/WEB-INF/jsp/template.jsp]] in DispatcherServlet with name 'dispatcher'
21:24:05.124 [http-bio-8080-exec-92] DEBUG o.s.web.servlet.view.JstlView - Forwarding to resource [/WEB-INF/jsp/template.jsp] in InternalResourceView 'template'
21:24:05.695 [http-bio-8080-exec-92] DEBUG o.s.web.servlet.DispatcherServlet - Successfully completed request

所以这一切都在这条线上进行,但现在我把web xml改成这样:

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

我重新部署并访问:

http://localhost:8080/contextroot/template

但我得到了一个400错误,以下是日志:

21:09:11.365 [http-bio-8080-exec-78] DEBUG o.s.web.servlet.DispatcherServlet - DispatcherServlet with name 'dispatcher' processing GET request for [/contextroot/template]
21:09:11.372 [http-bio-8080-exec-78] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Looking up handler method for path /template
21:09:11.383 [http-bio-8080-exec-78] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Returning handler method [public java.lang.String com.AnnaUnivResults.www.controller.ResultController.getPersonList(org.springframework.ui.ModelMap)]
21:09:11.384 [http-bio-8080-exec-78] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'resultController'
21:09:11.387 [http-bio-8080-exec-78] DEBUG o.s.web.servlet.DispatcherServlet - Last-Modified value for [/contextroot/template] is: -1
21:09:11.437 [http-bio-8080-exec-78] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Invoking afterPropertiesSet() on bean with name 'template'
21:09:11.440 [http-bio-8080-exec-78] DEBUG o.s.web.servlet.DispatcherServlet - Rendering view [org.springframework.web.servlet.view.JstlView: name 'template'; URL [/WEB-INF/jsp/template.jsp]] in DispatcherServlet with name 'dispatcher'
21:09:11.455 [http-bio-8080-exec-78] DEBUG o.s.web.servlet.view.JstlView - Forwarding to resource [/WEB-INF/jsp/template.jsp] in InternalResourceView 'template'
21:09:11.456 [http-bio-8080-exec-78] DEBUG o.s.web.servlet.DispatcherServlet - DispatcherServlet with name 'dispatcher' processing GET request for [/contextroot/WEB-INF/jsp/template.jsp]
21:09:11.457 [http-bio-8080-exec-78] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Looking up handler method for path /WEB-INF/jsp/template.jsp
21:09:11.459 [http-bio-8080-exec-78] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Did not find handler method for [/WEB-INF/jsp/template.jsp]
21:09:11.460 [http-bio-8080-exec-78] WARN  o.s.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/contextroot/WEB-INF/jsp/template.jsp] in DispatcherServlet with name 'dispatcher'
21:09:11.461 [http-bio-8080-exec-78] DEBUG o.s.web.servlet.DispatcherServlet - Successfully completed request
21:09:11.464 [http-bio-8080-exec-78] DEBUG o.s.web.servlet.DispatcherServlet - Successfully completed request

我敢肯定这是春天的虫子。我想在春季的网站上发布这篇文章,但显然春季的问题页面只是把你发送到StackOverflow,如果你问我的话,这有点欺骗性。

我想我的问题是,这是一个bug吗?

将/*更改为/

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <!--url-pattern>/*</url-pattern-->
    <url-pattern>/</url-pattern>
</servlet-mapping>

最新更新