spring boot打包成war包的頁面如何存放
背景
經(jīng)常有朋友問我,平時(shí)都是使用spring mvc,打包成war包發(fā)布到tomcat上,如何快速到切換到spring boot的war或者jar包上?
先來看看傳統(tǒng)的war包樣式是什么樣子的?
1. 傳統(tǒng)的spring MVC格式的war包

可以看到,webapp/resouces文件存放css/js/html等靜態(tài)文件,WEB-INF存放jsp動(dòng)態(tài)文件。
對(duì)應(yīng)的配置文件
@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;
}
}
對(duì)應(yīng)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的結(jié)構(gòu),spring 盡量避免jsp的動(dòng)態(tài)文件,而是使用如Thymeleaf 、FreeMarker等模板引擎,因?yàn)閖sp有很多限制。

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包

如何切換?
其實(shí),通過上面的結(jié)構(gòu),我們可以看出,spring boot的標(biāo)準(zhǔn)規(guī)格還是不建議使用jsp的,推薦使用Thymeleaf 、FreeMarker等模板引擎,然后所有的靜態(tài)文件同樣存儲(chǔ)在resources下面,可以使用代碼配置動(dòng)態(tài)代碼
@Configuration
@EnableWebMvc
public class SpringConfig
{
@Bean
public InternalResourceViewResolver viewResolver()
{
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/view/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
}
或者靜態(tài)屬性配置
spring.mvc.static-path-pattern=/resources/**
來自定義配置。
也可以使用靜態(tài)文件動(dòng)態(tài)化
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時(shí),編譯的jsp文件,上傳的文件等等默認(rèn)都存儲(chǔ)在臨時(shí)目錄里,會(huì)
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.
總結(jié)
以上所述是小編給大家介紹的spring boot打包成war包的頁面如何存放,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
相關(guān)文章
java框架基礎(chǔ)之SPI機(jī)制實(shí)現(xiàn)及源碼解析
這篇文章主要為大家介紹了java框架基礎(chǔ)之SPI機(jī)制實(shí)現(xiàn)及源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
java實(shí)現(xiàn)簡(jiǎn)單的圖書借閱系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)簡(jiǎn)單的圖書借閱系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
Java實(shí)現(xiàn)解析zip壓縮包并獲取文件內(nèi)容
這篇文章主要為大家詳細(xì)介紹了如何利用Java語言實(shí)現(xiàn)頁面上傳一個(gè)源碼壓縮包,后端將壓縮包解壓,并獲取每個(gè)文件中的內(nèi)容,感興趣的可以動(dòng)手嘗試一下2022-07-07
Spring Boot使用AOP實(shí)現(xiàn)REST接口簡(jiǎn)易靈活的安全認(rèn)證的方法
這篇文章主要介紹了Spring Boot使用AOP實(shí)現(xiàn)REST接口簡(jiǎn)易靈活的安全認(rèn)證的方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2018-11-11
Java使用Apache POI庫讀取Excel表格文檔的示例
POI庫是Apache提供的用于在Windows下讀寫各類微軟Office文檔的Java庫,這里我們就來看一下Java使用Apache POI庫讀取Excel表格文檔的示例:2016-06-06
MyBatis使用<foreach>標(biāo)簽like查詢報(bào)錯(cuò)解決問題
這篇文章主要介紹了MyBatis使用<foreach>標(biāo)簽like查詢報(bào)錯(cuò)解決問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
java實(shí)現(xiàn)簡(jiǎn)單的驗(yàn)證碼功能
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)簡(jiǎn)單的驗(yàn)證碼功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04

