基于前后端分离的项目,是不用后端模板引擎的,所以什么以.jsp,.vm结尾的模板引擎也是用不了的,前端开发做好的项目用webpack打包之后放在了后端项目里面,直接配置html视图解析器,然后放心给所有静态资源,这样就能够直接把静态文件打包之后加打war包直接放在服务器上了,废话不多说,办法如下:
第一步:配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>e3-manager</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- contextConfigLocation不是必须的, 如果不配置contextConfigLocation, springmvc的配置文件默认在:WEB-INF/servlet的name+"-servlet.xml" -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>e3-manager</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
这里说明一下,最好不要在servlet-mapping中配置资源过滤放行,原因很简单,Tomcat处理静态资源能力并不型,他是处理动态资源的能手。就这样。
第二步:在springmvc中配置html解析器
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".html"/>
<property name="contentType" value="text/html"/>
</bean>
第三步:在springmvc中设置静态资源放行规则:
<mvc:resources mapping="/css/**" location="/css/"/>
<mvc:resources mapping="/js/**" location="/js/"/>
<mvc:resources mapping="/fonts/**" location="/fonts/"/>
<mvc:resources mapping="/style/**" location="/style/"/>
或者直接所有静态资源直接全部放行:
<mvc:default-servlet-handler />
配置完了之后重启服务,就能访问你的静态页面。打包的时候直接打入war包,部署在服务器就行了!
转载:https://blog.csdn.net/Liuqz2009/article/details/100940411
查看评论