- 依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
- application配置
#是否缓存
spring.thymeleaf.cache=true
#html路径
spring.thymeleaf.prefix=classpath:/static/
#默认是HTML5 ,避HTML进行严格的检查的配置,当然你需要提前引入nekohtml依赖。
spring.thymeleaf.mode=LEGACYHTML5
#连接solr
spring.data.solr.host=http://192.168.241.130:8983/solr
- nekohtml依赖
<dependency>
<groupId>nekohtml</groupId>
<artifactId>nekohtml</artifactId>
<version>1.9.6.2</version>
</dependency>
- 创建 html
<!DOCTYPE html><html xmlns:th="http://www.thymeleaf.org"></html>
thymeleaf使用文本
语法 | 说明 |
---|---|
{home.welcome} | 使用国际化文本,国际化传参直接追加(value…) |
${user.name} | 使用会话属性 |
@{} | <link rel=“stylesheet” type=“text/css” media=“all"href=”…/…/css/gtvg.css" th:href="@{/css/gtvg.css}" /> |
– | – |
${} 中预存对象(表达式中基本对象) | |
param | 获取请求参数,比如${param.name},http://localhost:8080?name=jeff |
session | 获取 session 的属性 |
application | 获取 application 的属性 |
execInfo | 有两个属性 templateName和 now(是 java 的 Calendar 对象) |
– | – |
th扩展标签 | |
th:text | 普通字符串 |
th:utext | 转义文本 |
th:attr | <img src="…/…/images/gtvglogo.png" th:attr=“src=@{/images/gtvglogo.png},title=#{logo},alt=#{logo}” |
th:href | |
th:with | 定义常量 |
- 例子
<div th:text="${user.name}">
welcome to china
</div>
<div th:object="${user}">
<p th:text="*{name}"></p>
<p th:text="${session.user1.username}"></p>
</div>
<p th:text="${session.user1.toString()}"></p>
- 例子2
<form action="pr" method="get">
新闻:<input type="text" id="keyword" name="keyword"/>
<input type="submit" value="搜索">
</form>
<table>
<tr style="width:100%">
<th>id</th>
<th>国家</th>
<th>描述</th>
<th>年龄</th>
</tr>
<tr style='width:100%' th:each='person:${PersonList}'>
<td th:text="${person.id}"></td>
<td th:text="${person.country}"></td>
<td th:text="${person.desc}"></td>
<td th:text="${person.age}"></td>
</tr>
</table>
接口类
public interface PersonDao extends SolrCrudRepository<Person, String>{
public List<Person> findByDesc(String keyword,Sort sort);
@Query("desc_ik:?0")
public List<Person> query(String keyword);
}
- 控制层
@Controller
public class ThyController {
@GetMapping("/index")
public String index(Model model,HttpSession session) {
Map<String,String> map=new HashMap<String,String>();
map.put("id", "1001");
map.put("name", "张三");
model.addAttribute("user",map);
User user=new User("10","李四");
session.setAttribute("user1", user);
return "idx";
}
@Autowired
private PersonDao pd;
@GetMapping("/pr")
public String person(String keyword,Model model) {
List<Person> findByDesc = pd.findByDesc(keyword, new Sort(Direction.ASC,"age_i"));
model.addAttribute("PersonList",findByDesc);
return "person";
}
}
三目表达式,iterStat.odd判断是否是单行
th:each='person,iterStat:${PersonList}' th:style="${iterStat.odd} ? '' : 'background-color:red'"
- 模板布局
创建一个被包含的html
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" >
<title>Insert title here</title>
</head>
<body>
<div th:fragment="myheader">
首页 国内 国际 军事 财经 娱乐 体育 互联网 科技 游戏 女人 汽车 房产
</div>
</body>
</html>
包含的html
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<div th:include="header :: myheader"></div>
<p>