Thymeleaf变量表达式

35 篇文章 0 订阅
18 篇文章 3 订阅

Thymeleaf的用${...}来表示变量表达式,正常情况下,执行${...}时,实际上是针对context中的variables map执行OGNL表达式。在与SpringMVC集成的时候,用的是Spring EL。

<p>Today is: <span th:text="${today}">13 february 2011</span>.</p>

实际等效于执行了

ctx.getVariables().get("today");
<p th:utext="#{home.welcome(${session.user.name})}">
  Welcome to our grocery store, Sebastian Pepper!
</p>

等效于执行了

((User) ctx.getVariables().get("session").get("user")).getName();

因为底层实际上是执行的OGNL解析,所以${...}支持许多语法

/*
 * Access to properties using the point (.). Equivalent to calling property getters.
 */
${person.father.name}
/*
 * Access to properties can also be made by using brackets ([]) and writing 
 * the name of the property as a variable or between single quotes.
 */
${person['father']['name']}
/*
 * If the object is a map, both dot and bracket syntax will be equivalent to 
 * executing a call on its get(...) method.
 */
${countriesByCode.ES}
${personsByName['Stephen Zucchini'].age}
/*
 * Indexed access to arrays or collections is also performed with brackets, 
 * writing the index without quotes.
 */
${personsArray[0].name}
/*
 * Methods can be called, even with arguments.
 */
${person.createCompleteName()}
${person.createCompleteNameWithSeparator('-')}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值