三种取值表达式
- ${}:变量表达式(见下方使用)
- *{} :选择变量表达式(见下方使用)
- #{...} : Message 表达式,一般用于国际化的取值
前提:我们在model中存了一些数据(包括对象)
@RequestMapping("/testthymeleaf")
public String test2(Model model){
System.out.println("laidaole ");
model.addAttribute("welcome","springboot----hello"); //存入一个字符串
Hero hero = heroService.getHeroById(2);
System.out.println(hero);
model.addAttribute("hero",hero);//从数据库中取出一个对象存入model中
return "index";
}
在页面中取值(一般使用第一种方式更加方便明了)
在thymeleaf中,一种标签要在标签尖角括号内写th:xxx (前提要引入约束,见前一篇博客,引入约束后,如果存入的有值,那么写的时候也会自动提示)
<h5>取出存入的字符串</h5>
<p th:text="${welcome}">hello</p>
<hr>
<h5>取出对象第一种方式</h5>
<p th:text="${hero.username}"></p>
<p th:text="${hero.id}"></p>
<p th:text="${hero.phone}"></p>
<p th:text="${hero.profession}"></p>
<p th:text="${hero.email}"></p>
<p th:text="${hero.birthday}"></p>
<hr>
<h5>取出对象第二种方式</h5>
<div th:object="${hero}"> 先取出hero对象,然后再分别取出对象的属性
<p th:text="*{username}"></p>
<p th:text="*{id}"></p>
<p th:text="*{phone}"></p>
<p th:text="*{profession}"></p>
<p th:text="*{email}"></p>
<p th:text="*{birthday}"></p>
</div>