在Web開發過程當中,Spring Boot能夠經過@RestController來返回json數據,那如何渲染Web頁面?Spring Boot提供了多種默認渲染html的模板引擎,主要有如下幾種:html
Spring Boot 推薦使用這些模板引擎來代替 Jsp,Thymeleaf 只是其中一種,下面咱們來簡單聊聊Thymeleaf及實踐一下如何整合Spring Boot和Thymeleaf。java
Thymeleaf簡單的說,就是一款用於渲染 XML/XHTML/HTML5 內容的模板引擎,可用於Web與非Web環境中的應用開發。git
咱們以SpringBoot:1.開啓SpringBoot之旅的源碼做爲基礎修改,項目名爲:02.Spring-Boot-Thymeleaf 僅保留Application.java啓動類,其餘都去除。github
基本的目錄結構web
Application.javaspring
package com.w3cjava; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
pom.xmlapache
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.9.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.w3cjava</groupId> <artifactId>02.Spring-Boot-Thymeleaf</artifactId> <version>0.1</version> <name>02.Spring-Boot-Thymeleaf</name> <description>Thymeleaf project for Spring Boot</description> <properties> <java.version>1.8</java.version> <maven-jar-plugin.version>3.0.0</maven-jar-plugin.version> </properties> <dependencies> <!-- 支持web的模塊依賴 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <!-- 排除tomcat依賴 --> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <!-- jetty依賴 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency> <!-- 測試模塊依賴 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- 熱部署依賴 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
<!-- thymeleaf模板引擎依賴 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
package com.w3cjava.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class IndexController { @RequestMapping("/") public String index(ModelMap map) { // 加入一個屬性,用來在模板中讀取 map.addAttribute("host", "http://www.w3cjava.com"); // return模板文件的名稱,對應src/main/resources/templates/index.html return "index"; } }
在項目src/main/resources/templates目錄下新建一個模板文件index.html文件,內容以下json
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8" /> <title></title> </head> <body> <h1 th:text="${host}">Hello World</h1> </body> </html>
經過訪問路徑http://localhost:8080/ 結果頁面以下。tomcat
以上僅僅只是展現了Thymeleaf渲染文本的語法,更多Thymeleaf的頁面語法,還請訪問Thymeleaf的官方文檔查詢使用。微信
Thymeleaf給咱們提供部分參數的默認配置項,好比渲染模板默認路徑爲resources目錄下templates下的文件,文件類型爲text/html等等。
如須要修改默認配置,只需複製下面要修改的屬性到application.properties
中,並修改爲須要的值。
# Enable template caching. spring.thymeleaf.cache=true # Check that the templates location exists. spring.thymeleaf.check-template-location=true # Content-Type value. spring.thymeleaf.content-type=text/html # Enable MVC Thymeleaf view resolution. spring.thymeleaf.enabled=true # Template encoding. spring.thymeleaf.encoding=UTF-8 # Comma-separated list of view names that should be excluded from resolution. spring.thymeleaf.excluded-view-names= # Template mode to be applied to templates. See also StandardTemplateModeHandlers. spring.thymeleaf.mode=HTML5 # Prefix that gets prepended to view names when building a URL. spring.thymeleaf.prefix=classpath:/templates/ # Suffix that gets appended to view names when building a URL. spring.thymeleaf.suffix=.html spring.thymeleaf.template-resolver-order= # Order of the template resolver in the chain. spring.thymeleaf.view-names= # Comma-separated list of view names that can be resolved.
Spring Boot整合Thymeleaf比較簡單,採用了Spring Boot一向的作法,幾乎不用在配置文件中配置任何東西便可快速運行起來。
歡迎掃面下列二維碼關注「餘弦的自留地」公衆微信號萬物之中,但願至美