背景
经常有朋友问我, 平时都是使用 spring mvc, 打包成 war 包发布到 tomcat 上, 如何快速到切换到 spring boot 的 war 或者 jar 包上?
先来看看传统的 war 包样式是什么样子的?
1. 传统的 spring MVC 格式的 war 包
可以看到, webapp/resouces 文件存放 CSS/JS/html 等静态文件, Web-INF 存放 jsp 动态文件.
对应的配置文件
- @EnableWebMvc //mvc:annotation-driven
- @Configuration
- @ComponentScan({ "com.xxx.web" })
- public class SpringWebConfig extends WebMvcConfigurerAdapter {
- @Override
- public void addResourceHandlers(ResourceHandlerRegistry registry) {
- registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
- }
- @Bean
- public InternalResourceViewResolver viewResolver() {
- InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
- viewResolver.setViewClass(JstlView.class);
- viewResolver.setPrefix("/WEB-INF/views/jsp/");
- viewResolver.setSuffix(".jsp");
- return viewResolver;
- }
- }
对应 xml 的配置如下:
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:context="http://www.springframework.org/schema/context"
- 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.xsd
- http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context.xsd ">
- <context:component-scan base-package="com.xxxx.web" />
- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
- <property name="prefix" value="/WEB-INF/views/jsp/" />
- <property name="suffix" value=".jsp" />
- </bean>
- <mvc:resources mapping="/resources/**" location="/resources/" />
- <mvc:annotation-driven />
- </beans>
2.spring boot 格式的 jar 包
jar 的结构, spring 尽量避免 jsp 的动态文件, 而是使用如 Thymeleaf ,FreeMarker 等模板引擎, 因为 jsp 有很多限制.
- 28.4.5 JSP Limitations
- When running a Spring Boot application that uses an embedded servlet container (and is packaged as an executable archive), there are some limitations in the JSP support.
With Jetty and Tomcat, it should work if you use war packaging. An executable war will work when launched with java -jar, and will also be deployable to any standard container. JSPs are not supported when using an executable jar.
Undertow does not support JSPs.
Creating a custom error.jsp page does not override the default view for error handling. Custom error pages should be used instead.
3.spring boot 格式的 war 包
如何切换?
其实, 通过上面的结构, 我们可以看出, spring boot 的标准规格还是不建议使用 jsp 的, 推荐使用 Thymeleaf ,FreeMarker 等模板引擎, 然后所有的静态文件同样存储在 resources 下面, 可以使用代码配置动态代码
- @Configuration
- @EnableWebMvc
- public class SpringConfig
- {
- @Bean
- public InternalResourceViewResolver viewResolver()
- {
- InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
- viewResolver.setPrefix("/WEB-INF/view/");
- viewResolver.setSuffix(".jsp");
- return viewResolver;
- }
- }
或者静态属性配置
spring.mvc.static-path-pattern=/resources/**
来自定义配置.
也可以使用静态文件动态化
- spring.resources.chain.strategy.content.enabled=true
- spring.resources.chain.strategy.content.paths=/**
- spring.resources.chain.strategy.fixed.enabled=true
- spring.resources.chain.strategy.fixed.paths=/JS/lib/
- spring.resources.chain.strategy.fixed.version=v12
注意: CentOS 下使用 tomcat 时, 编译的 jsp 文件, 上传的文件等等默认都存储在临时目录里, 会
If you choose to use Tomcat on CentOS, be aware that, by default, a temporary directory is used to store compiled JSPs, file uploads, and so on. This directory may be deleted by tmpwatch while your application is running, leading to failures. To avoid this behavior, you may want to customize your tmpwatch configuration such that tomcat.* directories are not deleted or configure server.tomcat.basedir such that embedded Tomcat uses a different location.
参考资料
[1] https://www.mkyong.com/spring-boot/spring-boot-deploy-war-file-to-tomcat/
[2] https://www.baeldung.com/spring-boot-war-tomcat-deploy
[3] https://docs.spring.io/spring-boot/docs/2.1.2.RELEASE/reference/htmlsingle/#boot-features-spring-mvc-static-content
来源: https://www.cnblogs.com/davidwang456/p/11840357.html