/* * 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.map类型也支持 */ ${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 2 3 4 5 6 7
#ctx: the context object. #vars: the context variables. #locale: the context locale. #request: (only in Web Contexts) the HttpServletRequest object. #response: (only in Web Contexts) the HttpServletResponse object. #session: (only in Web Contexts) the HttpSession object. #servletContext: (only in Web Contexts) the ServletContext object.
例子:查看国家
1
Established locale country: <spanth:text="${#locale.country}">US</span>.
#execInfo: information about the template being processed. #messages: methods for obtaining externalized messages inside variables expressions, in the same way as they would be obtained using #{…} syntax. #uris: methods for escaping parts of URLs/URIs #conversions: methods for executing the configured conversion service (if any). #dates: methods for java.util.Date objects: formatting, component extraction, etc. #calendars: analogous to #dates, but for java.util.Calendar objects. #numbers: methods for formatting numeric objects. #strings: methods for String objects: contains, startsWith, prepending/appending, etc. #objects: methods for objects in general. #bools: methods for boolean evaluation. #arrays: methods for arrays. #lists: methods for lists. #sets: methods for sets. #maps: methods for maps. #aggregates: methods for creating aggregates on arrays or collections. #ids: methods for dealing with id attributes that might be repeated (for example, as a result of an iteration).
例子:进行日期格式化
1 2 3
<p> Today is: <spanth:text="${#calendars.format(today,'dd MMMM yyyy')}">13 May 2011</span> </p>
<!-- Will produce 'http://localhost:8080/gtvg/order/details?orderId=3' (plus rewriting) --> <ahref="details.html" th:href="@{http://localhost:8080/gtvg/order/details(orderId=${o.id})}">view</a>
<!-- Will produce '/gtvg/order/details?orderId=3' (plus rewriting) --> <ahref="details.html"th:href="@{/order/details(orderId=${o.id})}">view</a>
<!-- Will produce '/gtvg/order/3/details' (plus rewriting) --> <ahref="details.html"th:href="@{/order/{orderId}/details(orderId=${o.id})}">view</a>
Fragment inclusion和Fragment specification 实现了一个类似模板的功能。在我们的模板中,我们经常希望包含其他模板中的部分,例如页脚,页眉,菜单等部分。 为此,Thymeleaf提供了这个功能,只需要我们定义这些要包含的部分“片段”即可,定义使用th:fragment属性来完成。
<divth:switch="${user.role}"> <pth:case="'admin'">User is an administrator</p> <pth:case="#{roles.manager}">User is a manager</p> <pth:case="*">User is some other thing</p> </div>
@Repository publicclassStudentDao{ privatestatic Map<Integer, Student> students = null;
static { students = new HashMap<>(); students.put(101, new Student("张三",101)); students.put(102, new Student("李四",102)); students.put(103, new Student("王五",103)); students.put(104, new Student("赵六",104)); }
builder.append("<html><body><h1>Whitelabel Error Page</h1>").append("<p>This application has no explicit mapping for /error, so you are seeing this as a fallback.</p>").append("<div id='created'>").append(timestamp).append("</div>").append("<div>There was an unexpected error (type=").append(this.htmlEscape(model.get("error"))).append(", status=").append(this.htmlEscape(model.get("status"))).append(").</div>"); if (message != null) { builder.append("<div>").append(this.htmlEscape(message)).append("</div>"); }
if (trace != null) { builder.append("<div style='white-space:pre-wrap;'>").append(this.htmlEscape(trace)).append("</div>"); }