Thymeleaf用法2
官方文档:
https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#standard-expression-syntax
使用时需要为html添加属性:
<html xmlns:th="http://www.thymeleaf.org">
传输消息,(变量表达式)
controller层
@Controller
public class indexController {
@RequestMapping("/a")
public String test(Model model){
//属性名:msg,属性值hello Thymeleaf
model.addAttribute("msg","hello Thymeleaf");
return "test";
}
}
test.html页面
<!DOCTYPE html>
<html lang="en">
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<!-- th代表使用thyme leaf模板,${}变量表达式}-->
<div th:text="${msg}"></div>
</body>
</html>
<body>
<!-- msg:<h1>hello Thymeleaf</h1>-->
<div th:text="${msg}"></div>
<div th:utext="${msg}"></div>
</body>
结果:
遍历
<div th:each="fruit:${fruits}">[[${fruit}]]</div>
或者
<div th:each="fruit:${fruits}" th:text="${fruit}"></div>
@Controller
public class indexController {
@RequestMapping("/a")
public String test(Model model){
model.addAttribute("msg","<h1>hello Thymeleaf</h1>");
model.addAttribute("fruits", Arrays.asList("apple","orange","banana"));
return "test";
}
}
结果: