SpringMVC 静态资源过滤
1、在web.xml中配置 default Servletdefault*.cssdefault*.gifdefault*.jpgdefault*.png
1、在web.xml中配置 default Servlet
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.gif</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.jpg</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.png</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
2、mvc:default-servlet-handler
SpringMVC前端控制器
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
所有请求都走DispatcherServlet进行转换,但静态资源会被拦截
<mvc:default-servlet-handler/>
其代表的就是org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler
其作用就是对DispatcherServlet进行过滤,发现是静态资源就交给默认Servlet进行处理。
<mvc:default-servlet-handler default-servlet-name="resin-file"/> 在resin中使用
<mvc:default-servlet-handler default-servlet-name="default"/> 在tomcat/jetty中使用(与1步的效果一致)
其描述:
Configures a handler for serving static resources by forwarding to the Servlet container's default
Servlet. Use of this handler allows using a "/" mapping with the DispatcherServlet while still utilizing
the Servlet container to serve static resources. This handler will forward all requests to the default
Servlet. Therefore it is important that it remains last in the order of all other URL HandlerMappings.
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.
作用是配置静态资源处理器将请求转发到容器的默认Servelet进行处理,允许/ DispatcherServlet处理非静态请求,同时还利用默认Servlet为静态资源服务。
还有一个优先级的问题
3、Resource
<mvc:resources mapping="/image/**" location="/images/" />
<mvc:resources mapping="/js/**" location="/js/" />
<mvc:resources mapping="/css/**" location="/css/" />
<mvc:resources mapping="/html/**" location="/html/" />
<mvc:resources mapping="/software/**" location="/software/" /> MVC:Resouce 这个就自己定义处理静态文件的规则
<mvc:annotation-driven />默认配置了org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter
更多推荐



所有评论(0)