问题
springmvc添加<mvc:default-servlet-handler/>
或者<mvc:resources mapping="/img/**" location="/img/"/>
后,静态资源可以访问,Controller不能访问,删掉<mvc:default-servlet-handler/>
后才能访问controller
具体配置文件
<context:component-scan base-package="com.cw">
<!-- 只加载使用@Controller标注的bean -->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--
核心控制器拦截的是所有请求,需要对静态资源请求进行放行,通过配置放行资源实现
-->
<!--放行指定类型静态资源配置方式-->
<!--<mvc:resources mapping="/img/**" location="/img/"/>
<mvc:resources mapping="/js/**" location="/js/"/>
<mvc:resources mapping="/css/**" location="/css/"/>-->
<!--SpringMVC提供的通用资源放行方式,可以放行所有普通资源-->
<mvc:default-servlet-handler/>
如果想要解决访问静态资源问题,通常会使用默认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.
default-servlet-handler
将在SpringMVC上下文中定义一个DefaultServletHttpRequestHandler
。
DefaultServletHttpRequestHandler
的javadoc如下:
/**
* An {@link HttpRequestHandler} for serving static files using the Servlet container's "default" 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.
* 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.
*
* <p>Requests are handled by forwarding through the {@link RequestDispatcher} obtained via the
* name specified through the {@link #setDefaultServletName "defaultServletName" property}.
* In most cases, the {@code defaultServletName} does not need to be set explicitly, as the
* handler checks at initialization time for the presence of the default Servlet of well-known
* containers such as Tomcat, Jetty, Resin, WebLogic and WebSphere. However, when running in a
* container where the default Servlet's name is not known, or where it has been customized
* via server configuration, the {@code defaultServletName} will need to be set explicitly.
*
* @author Jeremy Grelle
* @author Juergen Hoeller
* @since 3.0.4
*/
翻译如下
/*使用servlet容器的默认servlet处理静态资源*/
An {@link HttpRequestHandler} for serving static files using the Servlet container's "default" Servlet.
/*当DispatcherServlet url-pattern为"/"时,该handler将会使用"/*"去匹配请求路径;因此,重置了servlet容器对静态资源的默认处理*/
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.