SpringBoot專案打成jar與war的區別
阿新 • • 發佈:2019-12-31
- SpringBoot預設支援很多模板引擎,但是JSP只能夠在War中使用,同時
mvc.view.prifix/suffix
必須主動配置給出,另外必須匯入JSP的預設渲染servlet:"org.apache.jasper.servlet.JspServlet"
,即新增依賴:
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope> provided</scope>
</dependency>
複製程式碼
- 無論是Jar還是War都能夠使用巢狀容器,
java -jar
來獨立執行 - 但只有war才能部署到外部容器中,且war中必須包含:
"src/main/webapp/WEB-INF/web.xml"
- SpringBoot中JSP模板引擎具備使用限制:
- jsp不能夠在jar中使用
- Udertow容器不支援Jsp
- 自定義的
error.jsp
錯誤頁面並不能夠複寫預設的error handling view
,如果你想要自定義錯誤頁面,請嘗試其他模板引擎Custom error pages - 如果你將專案打包成jar,就不要使用
src/main/webapp
Do not use the
src/main/webapp
directory if your application is packaged as ajar
. Although this directory is a common standard,it works only with war packaging,and it is silently ignored by most build tools if you generate a jar.
- SpringBoot的
歡迎頁
靜態資源
和模板引擎
,如果專案中不存在" "、"/"
的Handling
,那麼將優先檢視靜態資源位置中是否存在index.html
,否則才會檢視index模板
,如果都不存在
將使用預設歡迎頁
Spring Boot supports both static and templated welcome pages. It first looks for an index.html file in the configured static content locations. If one is not found,it then looks for an index template. If either is found,it is automatically used as the welcome page of the application.
-
Spring5.+
中與SpringMVC
擔負對等
作用的SpringWebFlux
並不是完全依賴於Servlet API
,所以不能將它打包成war
更不能使用src/main/webapp
目錄
Spring
WebFlux
applications do not strictly depend on theServlet API
,so they cannot be deployed as war files and do not use thesrc/main/webapp
directory.
- 使用者能夠直接訪問
src/main/webapp
中的靜態資源,但並不能直接訪問src/main/resources
中的靜態資源,但是Spring提供了ResourceHttpRequestHandler
來配置src/main/resources(classpath)
下指定訪問目錄 - MVC中
Interceptor
只能夠攔截Handling
與src/main/webapp
中的靜態資源,對src/main/resources
中的靜態資源無效 - 預設狀態下,使用者不具備
src/main/webapp/WEB-INF
的直接訪問許可權,但是可以通過程式中forward
、redirect
達到間接
訪問的目的,所以war專案中通常會將需要控制許可權的資原始檔放入到WEB-INF
中 - 另外可見:Spring工程訪問src/main/resources與src/main/webapp下靜態資源的區別