起因
spring MVC项目需要前后端分离,REST API 和 静态资源 需要走不同的DispatcherServlet。
静态资源目录如下:
目录结构
修改web.xml,添加不同的DispatcherServlet
restAppServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:rest-spring-mvc.xml
1
staticAppServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:static-spring-mvc.xml
2
<!-- 静态资源 Mapping (缺点:对不同格式文件需要增加配置) -->
<servlet-mapping>
<servlet-name>staticAppServlet</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>staticAppServlet</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>staticAppServlet</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>staticAppServlet</servlet-name>
<url-pattern>*.png</url-pattern>
</servlet-mapping>
<!-- REST API Mapping -->
<servlet-mapping>
<servlet-name>restAppServlet</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>restAppServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:rest-spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>staticAppServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:static-spring-mvc.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<!-- 静态资源 Mapping (缺点:对不同格式文件需要增加配置) -->
<servlet-mapping>
<servlet-name>staticAppServlet</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>staticAppServlet</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>staticAppServlet</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>staticAppServlet</servlet-name>
<url-pattern>*.png</url-pattern>
</servlet-mapping>
<!-- REST API Mapping -->
<servlet-mapping>
<servlet-name>restAppServlet</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
rest-spring-mvc.xml
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!-- 默认的注解映射的支持 -->
<mvc:annotation-driven />
<!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->
<context:component-scan base-package="controller package 的路径" />
<!-- 配置Jackson,用于编写REST API时,将Java Object映射成Json Obejct-->
<bean id="mappingJacksonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<bean class="org.springframework.http.MediaType">
<constructor-arg index="0" value="application" />
<constructor-arg index="1" value="json" />
<constructor-arg index="2" value="UTF-8" />
</bean>
</list>
</property>
</bean>
</beans>
static-spring-mvc.xml
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!-- 配置静态资源的映射路径 -->
<mvc:resources location="/resources/" mapping="/**"/>
</beans>
小结
如果使用默认的配置,Spring MVC DispatcherServlet 会监控 / 路径下的所有请求(包括了 /api/xxx 和 /index.html),这样子在静态资源的访问上会造成一定的麻烦,也不便于不同资源的维护。通过配置多个DispatcherServlet,将请求分开,就较好的解决了SpringMVC RESTAPI前后端分离资源访问的问题