1. 程式人生 > 程式設計 >SpringBoot專案打成jar與war的區別

SpringBoot專案打成jar與war的區別

  • 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
    目錄,儘管該目錄也是一個公共標準,但是它僅僅在war中有效,因為生成jar的構建工具將會自動把該目錄忽略

Do not use the src/main/webapp directory if your application is packaged as a jar. 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 the Servlet API,so they cannot be deployed as war files and do not use the src/main/webappdirectory.

  • 使用者能夠直接訪問src/main/webapp中的靜態資源,但並不能直接訪問src/main/resources中的靜態資源,但是Spring提供了ResourceHttpRequestHandler來配置src/main/resources(classpath)下指定訪問目錄
  • MVC中Interceptor只能夠攔截Handlingsrc/main/webapp中的靜態資源,對src/main/resources中的靜態資源無效
  • 預設狀態下,使用者不具備src/main/webapp/WEB-INF的直接訪問許可權,但是可以通過程式中forwardredirect達到間接訪問的目的,所以war專案中通常會將需要控制許可權的資原始檔放入到WEB-INF
  • 另外可見:Spring工程訪問src/main/resources與src/main/webapp下靜態資源的區別