Springboot內建Thymeleaf
網址
Thymeleaf 官網:
https://www.thymeleaf.org/Thymeleaf maven 位址:
https://mvnrepository.com/artifact/org.thymeleaf/thymeleafspring-boot-starter-thymeleaf maven 位址:
https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-thymeleaf內建
在 pom.xml 添加依賴
<!-- thymeleaf start -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>2.0.3.RELEASE</version>
</dependency>
<!-- thymeleaf end -->
————————————————
版權聲明:本文為CSDN部落客「Simba1949」的原創文章,遵循CC 4.0 BY-SA版權協定,轉載請附上原文出處連結及本聲明。
原文連結:https://blog.csdn.net/SIMBA1949/article/details/81043186
項目
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>
<groupId>top.simba1949</groupId>
<artifactId>Springboot-Thymeleaf</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- 繼承spring-boot-start-parent -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
</parent>
<!--配置管理-->
<properties>
<!--配置項目編碼-->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!--jdk編譯版本-->
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<!--依賴管理-->
<dependencies>
<!--springboot-web start-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--springboot-web end-->
<!--springboot test start-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--springboot test end-->
<!-- thymeleaf start -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>2.0.3.RELEASE</version>
</dependency>
<!-- thymeleaf end -->
</dependencies>
</project>
配置檔案
application.yml
server:
port: 8888
spring:
# thymeleaf 配置
thymeleaf:
# 字首配置
prefix: classpath:templates/
# 字尾配置
suffix: .html
# 類型配置
mode: HTML5
# thymeleaf 編碼配置
encoding: utf-8
servlet:
content-type: text/html
cache: false
Java 代碼
啟動類 App.class
package top.simba1949;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author [email protected]
* @date 2018/7/14 11:55
*/
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class,args);
}
}
web 層 UserController.class
package top.simba1949.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @author [email protected]
* @date 2018/7/14 11:56
*/
@RequestMapping("/user")
@Controller
public class UserController {
@GetMapping
public String string(Model model){
model.addAttribute("user","李白");
return "/user/userlist";
}
}
templates
在templates目錄下建立檔案夾user。
userlist.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Thymeleaf</title>
</head>
<body>
<p>君不見黃河之水天上來</p>
<p th:text="${user}"></p>
</body>
</html>
通路測試
http://localhost:8888/user