SpringBoot16:thymeleaf取值表达式

三种取值表达式

  • ${}:变量表达式(见下方使用)
  • *{} :选择变量表达式(见下方使用)
  • #{...} : 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>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值