SpringBoot——》thymeleaf
一、项目依赖配置
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
二、thymeleaf参数配置
参数对应jar包(版本可变动):spring-boot-autoconfigure-2.1.1.RELEASE.jar
参数对应java类:org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties
注:在 application.yml
或 application.property
中配置
spring:
# 模板引擎
thymeleaf:
# Thymeleaf3.0模板:HTML (默认)、XML、TEXT、JAVASCRIPT、CSS、RAW
mode: HTML
encoding: utf-8
# 禁用缓存:避免改了模板还要重启服务器
cache: false
# 资源信息
messages:
# 国际化资源文件路径
basename: static/i18n/messages
1、默认模板存放路径
默认值:classpath:/templates/
可修改:
spring:
thymeleaf:
prefix:/templates/
2、默认模板后缀
默认值:.html
可修改:
spring:
thymeleaf:
suffix:.html
3、默认静态资源路径 (4种)
classpath:/META-INF/resources/
classpath:/resources/
classpath:/static/
classpath:/public/
三、示例:Controller和index.html
在src/main/java/com/controller
下新建 WebController.java
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class WebController {
@GetMapping("/index")
public String index(){
return "index";
}
}
在 classpath:/resources/templates
下新建 index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>我是小仙</h1>
</body>
</html>