什么是Thymeleaf
Thymeleaf是一种Java XML / XHTML / HTML5模板引擎,可以在Web和非Web环境中使用。它更适合在基于MVC的Web应用程序的视图层提供XHTML / HTML5,但即使在脱机环境中,它也可以处理任何XML文件。它提供了完整的Spring Framework集成。
Thymeleaf 的基础使用
Thymeleaf的使用是由两部分组成的:标签 + 表达式,标签是Thymeleaf的语法结构,而表达式就是语法里的内容实现。
通过标签 + 表达式,让数据和模板结合,最终转换成html代码,返回给用户。
下面通过一个Springboot结合jpa加Thymeleaf显现一个简单的curd。
controller:
@Controller
public class UserController {
/**
* The User service.
*/
@Autowired
UserService userService;
/**
* Index string.
*
* @return the string
*/
@RequestMapping("/")
public String index() {
return "redirect:/list";
}
/**
* List string.
*
* @param model the model
* @return the string
*/
@RequestMapping("/list")
public String list(Model model) {
List<User> users = userService.getUserList();
model.addAttribute("users", users);
return "user/list";
}
/**
* To add string.
*
* @return the string
*/
@RequestMapping("/toAdd")
public String toAdd() {
return "us