第一步首先需要在pom.xml中导入两个依赖
spring管理版本号,所以不需要声明版本号,下面那个nekohtml是可以使用不那么严格的html语法
<!-- Thymeleaf模板引擎需要的两个依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
</dependency>
第二步在application.yml中配置相关配置
spring:
application:
name: spring-boot
thymeleaf:
cache: false #开发时关闭缓存,否则无法看到实时静态页面
mode: HTML #用非严格的HTML
encoding: UTF-8
servlet:
content-type: text/html
server:
port: 8081
user:
name: 老王
age: 20
address:
city: 广州
# 日志的配置 集成的框架为 logback 与log4j的作者为一个人
logging:
file:
# 存放日志文件的目录
name: ./logs/spring-boot-log
就是thymeleaf那一块几行配置,是配置在spring下
第三步创建一个html文件
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.css" rel="stylesheet">
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
<script src="https://cdn.bootcss.com/twitter-bootstrap/3.4.1/js/bootstrap.js"></script>
</head>
<body>
<!--/*@thymesVar id="name" type="com.smart.springboot.controller.TestThymeleaf"*/-->
<span th:text="${name}">李四</span>
</body>
</html>
第四步创建一个controller
package com.smart.springboot.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
/**
* @author Administrator
*/
@Controller
public class TestThymeleaf {
@GetMapping("/index")
public String testThy(Model model) {
model.addAttribute("name", "张三");
return "index";
}
}
controller那里是传值去前端,前端使用特定的语法接收.