如果在spring-mvc.xml 拦截方式如下配置: <servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 可以自定义servlet.xml配置文件的位置和名称,默认为WEB-INF目录下,名称为[<servlet-name>]-servlet.xml,如spring-servlet.xml
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-servlet.xml</param-value> 默认
</init-param>
-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- Spring配置 -->
<listener>
<listenerclass>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- 指定Spring Bean的配置文件所在目录。默认配置在WEB-INF目录下 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/applicationContext.xml</param-value>
</context-param>
那么,所有的请求都会被拦截。包括静态资源! '/' 将会替换掉容器的default servlet, 将会处理所有其他handler(Servlet)都不处理的访问请求.
如果web.xml没有配置其他特殊路径的servlet, 基本上所有的请求都交由DispatcherServlet处理.
将不会再访问容器中原始默认的servlet(你对静态资源的访问就是通过容器默认servlet处理的),故而静态资源将不可访问!!
如果想要解决访问静态资源问题,通常会使用默认handler: <mvc:default-servlet-handler/>
该标签的xsd文档说明如下: /*配置一个handler通过转发到servlet容器的默认servlet来处理静态资源*/
Configures a handler for serving static resources by forwarding to the Servlet container's default Servlet.
/*使用该handler允许DispatcherServlet 的url-patter 为'/',同时仍然使用servlet让其去处理静态资源*/
Use of this handler allows using a '/' mapping with the DispatcherServlet while still utilizing the Servlet container to serve static resources.
/*该handler将会转发所有的请求到默认serlvet*/
This handler will forward all requests to the default Servlet.
/*所以,在所有的URL HandlerMappings中,该handler对应的mapping应该留置最后!*/
Therefore it is important that it remains last in the order of all other URL HandlerMappings.
/*你可以使用两种方式去保证你的handlermapping 的order属性值小于DefaultServletHttpRequestHandler 对应的handlermapping的order属性值:使用<mvc:annotation-driven/>标签或者手动配置HandlerMapping实例并设置其order属性值*/
That will be the case if you use the 'annotation-driven' element
or alternatively if you are setting up your customized HandlerMapping instance
be sure to set its 'order' property to a value lower
than that of the DefaultServletHttpRequestHandler, which is Integer.MAX_VALUE.
新的问题出现了,你会发现以前的Controller不能访问了!!! 那么为什么会出现这个情况?
DefaultServletHttpRequestHandlerj的avadoc如下: /*使用servlet容器的默认servlet处理静态资源*/
* An {@link HttpRequestHandler} for serving static files using the Servlet container's 'default' Servlet.
*
/*当DispatcherServlet url-pattern为 ‘/’时,该handler将会使用‘/*’去匹配请求路径;因此,重置了servlet容器对静态资源的默认处理*/
* <p>This handler is intended to be used with a '/*' mapping when the
* {@link org.springframework.web.servlet.DispatcherServlet DispatcherServlet}
* is mapped to '/', thus overriding the Servlet container's default handling of static resources.
/*匹配该handler的mapping 应该是最后到达,当没有其他mapping可以处理请求时才会执行该handler匹配的mapping。*/
* The mapping to this handler should generally be ordered as the last in the chain so that it will
* only execute when no other more specific mappings (i.e., to controllers) can be matched.
即,当DispatcherServlet url-pattern为 ‘/’时,该handler将会使用‘/*’去匹配请求路径。 参考servlet的url-pattern规则可知 ‘/*’可以拦截一切请求。因为’/’将servlet定义为默认serlvet,在没有精确匹配servlet出现前,’ 这是很严重的, 也就是说,静态资源此时可以由默认default servlet进行处理,但是default servlet不能处理你的业务请求(mapping)!! 所以,需要保证该handler对应的handler mapping在执行顺序中为最后!!! 解决办法: <mvc:annotation-driven />
至于为什么这样解决,查看 点击查看 点击查看处理静态资源的几种方式springmvc处理静态资源的几种方式 【三种情况下的handlerAdapters:】
其中 AnnotationMethodHandlerAdapter是过期类,3.2之后被RequestMappingHandlerAdapter替代。 测试结果:1 3 可以正常流转,2 将找不到对应方法。 【Tips:】DispatcherServlet不拦截jsp请求 前面说的当DispatcherServlet配置为’/’, 将会覆盖default servlet, 将会处理所有其他Servlet都不处理的访问请求. 所以这里不拦截拦截.jsp, .jspx.的请求, 一定有其他地方拦截了该请求, 但是仔细查找web.xml并没有发现其他的servlet,那一定是在容器中定义的。 果不其然, 在%TOMCAT_HOME%/conf/web.xml中继承过来的JspServlet会处理该请求. 该xml配置了两个servlet: <servlet>
<servlet-name>default</servlet-name>
<servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>0</param-value>
</init-param>
<init-param>
<param-name>listings</param-name>
<param-value>false</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>jsp</servlet-name>
<servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
<init-param>
<param-name>fork</param-name>
<param-value>false</param-value>
</init-param>
<init-param>
<param-name>xpoweredBy</param-name>
<param-value>false</param-value>
</init-param>
<load-on-startup>3</load-on-startup>
</servlet>
<!-- The mapping for the default servlet -->
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- The mappings for the JSP servlet -->
<servlet-mapping>
<servlet-name>jsp</servlet-name>
<url-pattern>*.jsp</url-pattern>
<url-pattern>*.jspx</url-pattern>
</servlet-mapping>
这也就是为什么我们直接访问不在WEB-INF的jsp, 可以直接找到并解析的原因了. |
|
来自: Levy_X > 《JAVAWEB学习资料》