日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring Boot(三) 将war文件部署到tomcat 、 Thymeleaf示例

發布時間:2024/4/30 javascript 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring Boot(三) 将war文件部署到tomcat 、 Thymeleaf示例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Spring Boot(三) 將war文件部署到tomcat 、 Thymeleaf示例

一 、 將war文件部署到tomcat

  • 對于Spring Boot WAR部署,需要執行三個步驟:
  • 擴展SpringBootServletInitializer
  • 根據提供標記嵌入式servlet容器。
  • 更新包裝為 War
  • 測試工具
  • Spring Boot 1.4.2.RELEASE
  • Tomcat 8.5.9
  • Maven 3
  • 注:在Spring Boot中,具有嵌入服務器解決方案的最終可執行JAR文件可能不適合所有生產環境,特別是部署團隊(具有良好的服務器優化和監控技能知識,但缺乏開發經驗的團隊),他們希望完全控制服務器,并且不能接觸代碼。
  • 擴展SpringBootServletInitializer

  • 使現有的 @SpringBootApplication 類擴展 SpringBootServletInitializer

  • SpringBootWebApplication.java 文件內容如下:

    import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication public class SpringBootWebApplication {public static void main(String[] args) throws Exception {SpringApplication.run(SpringBootWebApplication.class, args);}}
  • SpringBoot war 部署

  • SpringBootWebApplication.java 文件的內容如下 -

    import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.support.SpringBootServletInitializer;@SpringBootApplication public class SpringBootWebApplication extends SpringBootServletInitializer {@Overrideprotected SpringApplicationBuilder configure(SpringApplicationBuilder application) {return application.sources(SpringBootWebApplication.class);}public static void main(String[] args) throws Exception {SpringApplication.run(SpringBootWebApplication.class, args);}}
  • 如果要為部署創建了一個額外的SpringBootWebApplication類,請確保告訴Spring Boot要從哪個主類開始啟動程序:在 pom.xml 文件中增加以下內容指定 -

    <properties><start-class>com.yiibai.NewSpringBootWebApplicationForWAR</start-class> </properties>
  • 提供標記嵌入式Servlet容器

  • 在 pom.xml 文件中增加以下內容指定 -

    <dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><!-- marked the embedded servlet container as provided --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId><scope>provided</scope></dependency></dependencies>
  • 更新包裝為War

  • 在pom.xml中添加

    <packaging>war</packaging>
  • 完成,mvn打包并將$project/target/xxx.war復制到Tomcat發布目錄進行部署。

  • 完整的Spring Boot War + Tomcat 部署

  • 以Spring Boot Thymeleaf為例,更新它并手動部署到Tomcat。
  • 更新現有的SpringBootWebApplication并讓它擴展SpringBootServletInitializer類
  • SpringBootWebApplication.java文件的完整代碼如下所示 -

    import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.support.SpringBootServletInitializer;@SpringBootApplication public class SpringBootWebApplication extends SpringBootServletInitializer {@Overrideprotected SpringApplicationBuilder configure(SpringApplicationBuilder application) {return application.sources(SpringBootWebApplication.class);}public static void main(String[] args) throws Exception {SpringApplication.run(SpringBootWebApplication.class, args);}}
  • 更新打包包,并按提供的標記spring-boot-starter-tomcat。pom.xml文件的完整代碼如下所示 -

    <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><artifactId>spring-boot-web-thymeleaf</artifactId><packaging>war</packaging><name>Spring Boot Web Thymeleaf Example</name><description>Spring Boot Web Thymeleaf Example</description><url>http://www.yiibai.com</url><version>1.0</version><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.4.2.RELEASE</version></parent><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><!-- marked the embedded servlet container as provided --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId><scope>provided</scope></dependency><!-- hot swapping, disable cache for template, enable live reload --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><optional>true</optional></dependency><!-- Optional, for bootstrap --><dependency><groupId>org.webjars</groupId><artifactId>bootstrap</artifactId><version>3.3.7</version></dependency></dependencies><build><plugins><!-- Package as an executable jar/war --><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build> </project>
  • 這是可選的,將contextPath更新為/yiibai以方便稍后作為演示。準備好WAR部署文件。application.properties 文件的完整內容如下所示 -

    welcome.message: Hello Yiibaiserver.contextPath=/yiibai
  • 使用maven執行打包 ,后在target目錄下得到一個war文件 。 放到Tomcat的webapps目錄下 開啟tomcat即可
  • 訪問:http://localhost:8080/yiibai/ ,程序沒有錯誤問題,應該會看到以下結果 -
  • 二 、Thymeleaf示例

  • 這是一個Spring Boot Web應用示例,使用嵌入式Tomcat + Thymeleaf模板引擎,并將其作為可執行JAR文件。
  • 使用相關技術
  • Spring Boot 1.4.2.RELEASE
  • Spring 4.3.4.RELEASE
  • Thymeleaf 2.1.5.RELEASE
  • Tomcat Embed 8.5.6
  • Maven 3
  • Java 8
  • 項目目錄
  • 項目依賴

  • 聲明 spring-boot-starter-thymeleaf以獲得開發Spring + Thymeleaf Web應用程序所需的任何程序類庫。
  • pom.xml文件內容:

    <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><artifactId>spring-boot-web-thymeleaf</artifactId><packaging>jar</packaging><name>Spring Boot Web Thymeleaf 示例</name><description>Spring Boot Web Thymeleaf 示例描述</description><url>http://www.yiibai.com</url><version>1.0</version><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.4.2.RELEASE</version></parent><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><!-- hot swapping, disable cache for template, enable live reload --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><optional>true</optional></dependency><!-- Optional, for bootstrap --><dependency><groupId>org.webjars</groupId><artifactId>bootstrap</artifactId><version>3.3.7</version></dependency></dependencies><build><plugins><!-- Package as an executable jar/war --><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build> </project>
  • 顯示項目依賴關系:
  • 提示: spring-boot-devtools這個spring-boot-devtools有助于禁用緩存并啟用熱插拔,以便開發人員總是看到最后的更改。 有利于發展。 閱讀這篇 - Spring Boot - 開發工具。嘗試修改Thymeleaf模板或屬性文件,刷新瀏覽器以查看更改立即生效。
  • Spring Boot

  • 使用@SpringBootApplication進行注釋。 運行此類來啟動Spring Boot Web應用程序。

  • SpringBootWebApplication.java

    import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication public class SpringBootWebApplication {public static void main(String[] args) throws Exception {SpringApplication.run(SpringBootWebApplication.class, args);}}
  • 一個簡單的Spring控制器類: WelcomeController.java 代碼如下 -

    import java.util.Map;import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping;@Controller public class WelcomeController {// inject via application.properties@Value("${welcome.message:test}")private String message = "Hello World";@RequestMapping("/")public String welcome(Map<String, Object> model) {model.put("message", this.message);return "welcome";}}
  • Thymeleaf+資源+靜態文件

  • 對于Thymeleaf模板文件,放入 src/main/resources/templates/ 目錄下, src/main/resources/templates/welcome.html 文件代碼如下 -

    <!DOCTYPE HTML> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>Spring Boot Thymeleaf Hello World示例</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><link rel="stylesheet" type="text/css"href="webjars/bootstrap/3.3.7/css/bootstrap.min.css" /><link rel="stylesheet" th:href="@{/css/main.css}"href="../../css/main.css" /></head> <body><nav class="navbar navbar-inverse"><div class="container"><div class="navbar-header"><a class="navbar-brand" href="#">Spring Boot</a></div><div id="navbar" class="collapse navbar-collapse"><ul class="nav navbar-nav"><li class="active"><a href="#">首頁</a></li><li><a href="#about">關于</a></li></ul></div></div></nav><div class="container"><div class="starter-template"><h1>Spring Boot Web Thymeleaf示例</h1><h2><span th:text="'Message: ' + ${message}"></span></h2></div></div><!-- /.container --><script type="text/javascript"src="webjars/bootstrap/3.3.7/js/bootstrap.min.js"></script></body> </html>
  • 對于靜態文件,如CSS或Javascript,將它們放入 /src/main/resources/static/ 目錄, /src/main/resources/static/css/main.css文件代碼如下 -

    h1{font-size: 20pt; } h2{font-size: 16pt; }
  • 對于屬性文件,放入 /src/main/resources/ 目錄中, /src/main/resources/application.properties 文件中的代碼內容如下 -

    welcome.message: Hello, Spring Boot
  • 運行
  • 啟動Spring Boot Web應用程序,使用Maven命令:mvn spring-boot:run,運行結果輸出結果如下 - 可以再瀏覽器中訪問localhost:8080測試
  • 創建可執行jar
  • 打包項目以創建可執行的JAR文件。使用Maven命令:mvn clean package,
  • 執行可執行jar:F:\worksp\springboot\spring-boot-web-thymeleaf> java -jar target/spring-boot-web-thymeleaf-1.0.jar
  • 在瀏覽器中訪問localhost:8080測試
  • 與50位技術專家面對面20年技術見證,附贈技術全景圖

    總結

    以上是生活随笔為你收集整理的Spring Boot(三) 将war文件部署到tomcat 、 Thymeleaf示例的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。