//将HTML页面放到classpath:/templates/下面面,thymeleaf即可渲染页面
@ConfigurationProperties(
prefix = "spring.thymeleaf"
)
public class ThymeleafProperties {
private static final Charset DEFAULT_ENCODING;
public static final String DEFAULT_PREFIX = "classpath:/templates/";
public static final String DEFAULT_SUFFIX = ".html";
private boolean checkTemplate = true;
private boolean checkTemplateLocation = true;
private String prefix = "classpath:/templates/";
private String suffix = ".html";
语法
1导入名称空间
<html xmlns:th="http://www.thymeleaf.org">//会有语法提示
bug来的莫名奇妙,抓狂。。。
spring boot需要在setting中修改本地仓库的位置
//使用restcontroller页面显示的是return的值
@RestController
public class TestController {
//classpath:/templates/success
@RequestMapping("/success")
public String success(Map<String,Object> map){
map.put("hello","你好");
return "success";
}
}
以下
@Controller
public class TestController {
//classpath:/templates/success
@RequestMapping("/success")
public String success(Map<String,Object> map){
map.put("hello","你好");
return "success";
}
}
//会取出hello的值,只显示数据内容,并不显示前端页面所写的内容
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>测试页面</title>
</head>
<body>
<p th:text="${hello}">测试一下</p>//会将p标签里面的内容覆盖掉
<p>测试一下</p>//成功显示
</body>
</html>
一开始使用controller注解并不能访问页面,后来莫名奇妙的好了。。。以及不知道为啥要关闭cache(貌似springboot的版本控制中thymeleaf已经更新到3.0以上了,不需要在pom文件的properties中修改版本)
语法规则
1)th:text;改变当前元素里面的文本内容
th: 任意html属性,来替换原生属性
2)表达式
参考官方文档:
Standard Expression Syntax