Thymeleaf开箱即用的特性。它提供标准和spring标准两种方言,可以直接套用模板实现JSTL、OGNL表达式效果,避免每天套模板、改jstl、改标签的困扰。同时开发人员也可以扩展和创建自定义的方言。
Thymeleaf整合SpringBoot
这篇查询添加是基于上一篇spring boot整合mybatis做的哦,需要看完整的添加删除的小伙伴可以看下上一篇的哦
- 在pom.xml文件引入thymeleaf
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
- controller
@Controller
public class DeptController {
@Autowired
private DeptService deptService;
/**
* 查询
* @param model
* @return
*/
@RequestMapping("/")
public String findAll(Model model){
List<Dept> allDept = deptService.findAllDept();
model.addAttribute("dept",allDept);
return "index";
}
/**
* 删除
* @param deptno
* @return
*/
@RequestMapping("/deleteDept")
public String deleteDept(Integer deptno){
int i = deptService.deleteById(deptno);
return "redirect:/";
}
/**
* 添加
* @param dept
* @return
*/
@RequestMapping("/addDept")
public String addDept(Dept dept){
int i = deptService.addDept(dept);
return "redirect:/";
}
}
- 新建编辑模板文件,在resources文件夹下的templates目录,用于存放HTML等模板文件,在这新增indexhtml,添加如下代码。
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<table border="1" cellspacing="0">
<tr>
<td>编号</td>
<td>名称</td>
<td>地址</td>
<td>操作</td>
</tr>
<tr th:each="dept:${dept}">
<td th:text="${dept.deptno}"></td>
<td th:text="${dept.dname}"></td>
<td th:text="${dept.loc}"></td>
<td><a th:href="@{/deleteDept(deptno=${dept.deptno})}" th:text="删除"></td>
</tr>
</table>
<hr>
<form th:action="@{/addDept}" th:method="post">
名称:<input type="text" th:name="dname"/></br>
地址:<input type="text" th:name="loc"/></br>
<input type="submit" value="添加">
</form>
</body>
</html>
效果图