Directory structure shown in Figure
index.html
<!DOCTYPE html>
<html lang="en"
xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<script>
</script>
<body>
<div th:width="300px" th:height="50px">
<a th:href="@{/edit(user_id=${null})}"> 添加新数据 </a>
</div>
<table border="1">
<tr>
<th>id</th>
<th>姓名</th>
<th>年龄</th>
<th>修改</th>
<th>删除</th>
</tr>
<tr th:each="student:${students}">
<td><span th:text="${student.id}"></span></td>
<td><span th:text="${student.name}"></span></td>
<td><span th:text="${student.age}"></span></td>
<td><a th:href="@{/edit(id=${student.id})}"> edit </a></td>
<td><a th:href="@{/del(user_id=${student.id})}"> delete </a></td>
</tr>
</table>
</body>
</html>
add.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>新增、编辑客户</title>
</head>
<body>
<form th:action="@{/save}" method="post">
<div>
<label>id</label>
<input type="text" name="id" readonly="readonly" th:field="${student.id}" />
</div>
<div>
<label>name</label>
<input type="text" name="name" th:field="${student.name}" />
</div>
<div>
<label>age</label>
<input type="text" name="age" th:field="${student.age}" />
</div>
<div>
<input type="submit" value="提交" />
</div>
</form>
</body>
</html>
StudentHtmlController
这里使用@Controller ,不再使用@RestController
@Controller
@RequestMapping(path = "/")
public class StudentHtmlController {
@Autowired
StudentService studentService;
@RequestMapping(path = "/index" , method = RequestMethod.GET)
public String getHtml(Model model){
model.addAttribute("students" ,studentService.findAll());
return "index";
}
@RequestMapping(path = "/del")
public String del(@RequestParam(name = "user_id") Integer user_id){
studentService.delById(user_id);
return "redirect:/index";
}
@RequestMapping(path = "/save" ,method = RequestMethod.POST)
public String save(@ModelAttribute Student student){
if(student==null){
return "fail";
}
if(student.id!=null && student.id > 0){
studentService.update(student);
return "redirect:/index";
}else{
studentService.create(student);
return "redirect:/index";
}
}
@RequestMapping(path = "/findById" ,method = RequestMethod.GET)
public Student findById(@RequestParam("id") Integer id){
return studentService.findById(id);
}
@RequestMapping(path = "/edit" , method = RequestMethod.GET)
public String edit(ModelMap modelMap ,@RequestParam(defaultValue = "0") int id){
if(id>0){
modelMap.addAttribute("student",studentService.findById(id));
}else{
Student student=new Student();
student.setAge(null);
student.setName("");
modelMap.addAttribute("student",student);
}
return "update";
}
}
ThymeleafApplication
使用@MapperScan 扫描Mapper 包路径
package com.gali.thymeleaf;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.gali.thymeleaf.mapper")
public class ThymeleafApplication {
public static void main(String[] args) {
SpringApplication.run(ThymeleafApplication.class, args);
}
}
效果图
完整Code(thymeleaf)
链接:https://pan.baidu.com/s/1wgTdmaNlO00Np2AlqEiSNg
提取码:qrvq