javascript
Spring Boot(三) 将war文件部署到tomcat 、 Thymeleaf示例
Spring Boot(三) 將war文件部署到tomcat 、 Thymeleaf示例
一 、 將war文件部署到tomcat
擴展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 部署
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二 、Thymeleaf示例
項目依賴
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
使用@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(三) 将war文件部署到tomcat 、 Thymeleaf示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring Boot(二)应用实例
- 下一篇: Spring Boot(四)Spring