1. 程式人生 > 程式設計 >SpringBoot:2.SpringBoot整合Thymeleaf模板引擎渲染web檢視

SpringBoot:2.SpringBoot整合Thymeleaf模板引擎渲染web檢視

在Web開發過程中,Spring Boot可以通過@RestController來返回json資料,那如何渲染Web頁面?Spring Boot提供了多種預設渲染html的模板引擎,主要有以下幾種:

  • Thymeleaf
  • FreeMarker
  • Velocity
  • Groovy
  • Mustache

Spring Boot 推薦使用這些模板引擎來代替 Jsp,Thymeleaf 只是其中一種,下面我們來簡單聊聊Thymeleaf及實踐一下如何整合Spring Boot和Thymeleaf。

1.Thymeleaf 介紹

Thymeleaf簡單的說,就是一款用於渲染 XML/XHTML/HTML5 內容的模板引擎,可用於Web與非Web環境中的應用開發。

2.實踐Spring Boot整合Thymeleaf

2.1 構建Spring Boot專案

我們以SpringBoot:1.開啟SpringBoot之旅的原始碼作為基礎修改,專案名為:02.Spring-Boot-Thymeleaf 僅保留Application.java啟動類,其他都去除。

基本的目錄結構

在這裡插入圖片描述

Application.java

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.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 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> 複製程式碼

2.2 引入Thymeleaf依賴

<!-- thymeleaf模板引擎依賴 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>	
複製程式碼

2.3 構建IndexController

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";
	}

}
複製程式碼

2.4 渲染頁面

在專案src/main/resources/templates目錄下新建一個模板檔案index.html檔案,內容如下

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8" />
    <title></title>
</head>
<body>
<h1 th:text="${host}">Hello World</h1>
</body>
</html>
複製程式碼

2.5 訪問路徑

通過訪問路徑http://localhost:8080/ 結果頁面如下。

在這裡插入圖片描述

以上僅僅只是展示了Thymeleaf渲染文字的語法,更多Thymeleaf的頁面語法,還請訪問Thymeleaf的官方檔案查詢使用。

2.6 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.
複製程式碼

3.小結

Spring Boot整合Thymeleaf比較簡單,採用了Spring Boot一貫的做法,幾乎不用在配置檔案中配置任何東西即可快速執行起來。

原始碼:02.Spring-Boot-Thymeleaf

歡迎掃面下列二維碼關注“餘弦的自留地”公眾微訊號

在這裡插入圖片描述

萬物之中,希望至美