Wetts's blog

Stay Hungry, Stay Foolish.

0%

SpringMVC-标签-《mvc:resources》

转自:http://www.jianshu.com/p/d8aee77e5f3a

spring mvc 的<mvc:resources mapping="***" location="***">标签是用来进行配置静态资源访问的。

1
2
3
4
5
6
7
8
9
10
<servlet>  
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

spring mvc会在WEB-INF下扫描一个springMVC-servlet.xml文件,如果没有提供,将会报一个文件找不到的异常。
由于spring mvc拦截了所有请求

1
2
3
4
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

访问静态资源文件的时候也会被DispatcherServlet拦截,而且会进行一系列复杂的处理,所以对静态资源必须进行特殊的配置。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

<mvc:resources mapping="/javascript/**" location="/static_resources/javascript/"/>
<mvc:resources mapping="/styles/**" location="/static_resources/css/"/>
<mvc:resources mapping="/images/**" location="/static_resources/images/"/>
<mvc:default-servlet-handler />


<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>