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.html

  • 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.java

  • 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.web

  • 用戶可以直接訪問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下靜態資源的區別
相關文章
相關標籤/搜索