thymeleaf 标准表达式
${..}:读取对象
*{..}:${..}功能增强,还可读取当前对象
#{..}:读取属性文件,常用于读取国际化文件
<span th:text="#{hello}"></span> //读取国际化属性文件中key:hello的value
@{..}:url链接、本地文件链接
<script th:src="@{/jquery/jquery-3.5.1.min.js}"></script> //导入 jquery 文件
<a th:href="@{http://www.baidu.com}">百度一下</a><br> //超链接
~{..}:引入页面模板
<div th:insert="~{commons :: main}">...</div> //插入commons.html 中 th:fragment为main的局部页面
*******************
示例
***************
controller 层
HelloController
@RestController
public class HelloController {
@RequestMapping("/hello")
public ModelAndView hello(ModelAndView mv){
Person person=new Person();
person.setId(1);
person.setName("瓜田李下");
person.setAge(20);
mv.addObject("person",person);
mv.setViewName("test");
return mv;
}
}
***************
前端页面
test.html
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org" xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div th:align="center" style="color: orange">
<strong>
<span th:text="${person.id}"></span> ==>
<span th:text="${person.name}"></span> ==>
<span th:text="${person.age}"></span>
</strong>
</div><br>
<div th:align="center" style="color: aquamarine" th:object="${person}">
<strong>
<span th:text="*{id}"></span> ==>
<span th:text="*{name}"></span> ==>
<span th:text="*{age}"></span>
</strong>
</div><br>
<div th:align="center" style="color: purple">
<strong>
<span th:text="*{person.id}"></span> ==>
<span th:text="*{person.name}"></span> ==>
<span th:text="*{person.age}"></span>
</strong>
</div>
</body>
</html>
*******************
使用测试
localhost:8080/hello