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('-')}