thymeleaf 语法学习
分类:软件编程
阅读:481
作者:马晶晶
发布:2016-12-07 17:29:03

一.简单表达格式:
  1.变量的表达式:${…}
  2.选择变量表达式:*{…}
  3.信息表达:#{…}
  4.链接URL表达式:@{…}
二.字面值
  1.文本文字:‘one text‘, ‘Another one!‘,…
  2.文字数量:0, 34, 3.0, 12.3,…
  3.布尔型常量:true, false
  4.空的文字:null
  5.文字标记:one, sometext, main,…
三:文本处理

  1.字符串并置:+

  2.文字替换:|The name is ${name}|

四.表达基本对象

  1.#ctx:上下文对象

  2.#vars:上下文变量

  3.#locale:上下文语言环境

  4.#httpServletRequest:(只有在Web上下文)HttpServletRequest对象

  5.#httpSession:(只有在Web上下文)HttpSession对象。
用法:

  1. <span th:text="${#locale.country}">US</span>

六.实用表达对象 

  • #dates: java.util的实用方法。对象:日期格式、组件提取等.
  • #calendars: 类似于#日期,但对于java.util。日历对象
  • #numbers: 格式化数字对象的实用方法。
  • #strings:字符串对象的实用方法:包含startsWith,将/附加等。
  • #objects: 实用方法的对象。
  • #bools: 布尔评价的实用方法。
  • #arrays: 数组的实用方法。
  • #lists: list集合。
  • #sets:set集合。
  • #maps: map集合。
  • #aggregates: 实用程序方法用于创建聚集在数组或集合.
  • #messages: 实用程序方法获取外部信息内部变量表达式,以同样的方式,因为他们将获得使用# {…}语法
  • #ids: 实用程序方法来处理可能重复的id属性(例如,由于迭代)。
  1. #dates : utility methods for java.util.Date objects:
  2. /*
  3. * ======================================================================
  4. * See javadoc API for class org.thymeleaf.expression.Dates
  5. * ======================================================================
  6. */
  7. /*
  8. * Null-safe toString()
  9. */
  10. ${#strings.toString(obj)} // also array*, list* and set*
  11. /*
  12. * Format date with the standard locale format
  13. * Also works with arrays, lists or sets
  14. */
  15. ${#dates.format(date)}
  16. ${#dates.arrayFormat(datesArray)}
  17. ${#dates.listFormat(datesList)}
  18. ${#dates.setFormat(datesSet)}
  19. /*
  20. * Format date with the specified pattern
  21. * Also works with arrays, lists or sets
  22. */
  23. ${#dates.format(date, ‘dd/MMM/yyyy HH:mm‘)}
  24. ${#dates.arrayFormat(datesArray, ‘dd/MMM/yyyy HH:mm‘)}
  25. ${#dates.listFormat(datesList, ‘dd/MMM/yyyy HH:mm‘)}
  26. ${#dates.setFormat(datesSet, ‘dd/MMM/yyyy HH:mm‘)}
  27. /*
  28. * Obtain date properties
  29. * Also works with arrays, lists or sets
  30. */
  31. ${#dates.day(date)} // also arrayDay(...), listDay(...), etc.
  32. ${#dates.month(date)} // also arrayMonth(...), listMonth(...), etc.
  33. ${#dates.monthName(date)} // also arrayMonthName(...), listMonthName(...), etc.
  34. ${#dates.monthNameShort(date)} // also arrayMonthNameShort(...), listMonthNameShort(...), etc.
  35. ${#dates.year(date)} // also arrayYear(...), listYear(...), etc.
  36. ${#dates.dayOfWeek(date)} // also arrayDayOfWeek(...), listDayOfWeek(...), etc.
  37. ${#dates.dayOfWeekName(date)} // also arrayDayOfWeekName(...), listDayOfWeekName(...), etc.
  38. ${#dates.dayOfWeekNameShort(date)} // also arrayDayOfWeekNameShort(...), listDayOfWeekNameShort(...), etc.
  39. ${#dates.hour(date)} // also arrayHour(...), listHour(...), etc.
  40. ${#dates.minute(date)} // also arrayMinute(...), listMinute(...), etc.
  41. ${#dates.second(date)} // also arraySecond(...), listSecond(...), etc.
  42. ${#dates.millisecond(date)} // also arrayMillisecond(...), listMillisecond(...), etc.
  43. /*
  44. * Create date (java.util.Date) objects from its components
  45. */
  46. ${#dates.create(year,month,day)}
  47. ${#dates.create(year,month,day,hour,minute)}
  48. ${#dates.create(year,month,day,hour,minute,second)}
  49. ${#dates.create(year,month,day,hour,minute,second,millisecond)}
  50. /*
  51. * Create a date (java.util.Date) object for the current date and time
  52. */
  53. ${#dates.createNow()}
  54. /*
  55. * Create a date (java.util.Date) object for the current date (time set to 00:00)
  56. */
  57. ${#dates.createToday()}
  58. Calendars
  59. #calendars : analogous to #dates, but for java.util.Calendar objects:
  60. /*
  61. * ======================================================================
  62. * See javadoc API for class org.thymeleaf.expression.Calendars
  63. * ======================================================================
  64. */
  65. /*
  66. * Format calendar with the standard locale format
  67. * Also works with arrays, lists or sets
  68. */
  69. ${#calendars.format(cal)}
  70. ${#calendars.arrayFormat(calArray)}
  71. ${#calendars.listFormat(calList)}
  72. ${#calendars.setFormat(calSet)}
  73. /*
  74. * Format calendar with the specified pattern
  75. * Also works with arrays, lists or sets
  76. */
  77. ${#calendars.format(cal, ‘dd/MMM/yyyy HH:mm‘)}
  78. ${#calendars.arrayFormat(calArray, ‘dd/MMM/yyyy HH:mm‘)}
  79. ${#calendars.listFormat(calList, ‘dd/MMM/yyyy HH:mm‘)}
  80. ${#calendars.setFormat(calSet, ‘dd/MMM/yyyy HH:mm‘)}
  81. /*
  82. * Obtain calendar properties
  83. * Also works with arrays, lists or sets
  84. */
  85. ${#calendars.day(date)} // also arrayDay(...), listDay(...), etc.
  86. ${#calendars.month(date)} // also arrayMonth(...), listMonth(...), etc.
  87. ${#calendars.monthName(date)} // also arrayMonthName(...), listMonthName(...), etc.
  88. ${#calendars.monthNameShort(date)} // also arrayMonthNameShort(...), listMonthNameShort(...), etc.
  89. ${#calendars.year(date)} // also arrayYear(...), listYear(...), etc.
  90. ${#calendars.dayOfWeek(date)} // also arrayDayOfWeek(...), listDayOfWeek(...), etc.
  91. ${#calendars.dayOfWeekName(date)} // also arrayDayOfWeekName(...), listDayOfWeekName(...), etc.
  92. ${#calendars.dayOfWeekNameShort(date)} // also arrayDayOfWeekNameShort(...), listDayOfWeekNameShort(...), etc.
  93. ${#calendars.hour(date)} // also arrayHour(...), listHour(...), etc.
  94. ${#calendars.minute(date)} // also arrayMinute(...), listMinute(...), etc.
  95. ${#calendars.second(date)} // also arraySecond(...), listSecond(...), etc.
  96. ${#calendars.millisecond(date)} // also arrayMillisecond(...), listMillisecond(...), etc.
  97. /*
  98. * Create calendar (java.util.Calendar) objects from its components
  99. */
  100. ${#calendars.create(year,month,day)}
  101. ${#calendars.create(year,month,day,hour,minute)}
  102. ${#calendars.create(year,month,day,hour,minute,second)}
  103. ${#calendars.create(year,month,day,hour,minute,second,millisecond)}
  104. /*
  105. * Create a calendar (java.util.Calendar) object for the current date and time
  106. */
  107. ${#calendars.createNow()}
  108. /*
  109. * Create a calendar (java.util.Calendar) object for the current date (time set to 00:00)
  110. */
  111. ${#calendars.createToday()}
  112. Numbers
  113. #numbers : utility methods for number objects:
  114. /*
  115. * ======================================================================
  116. * See javadoc API for class org.thymeleaf.expression.Numbers
  117. * ======================================================================
  118. */
  119. /*
  120. * ==========================
  121. * Formatting integer numbers
  122. * ==========================
  123. */
  124. /*
  125. * Set minimum integer digits.
  126. * Also works with arrays, lists or sets
  127. */
  128. ${#numbers.formatInteger(num,3)}
  129. ${#numbers.arrayFormatInteger(numArray,3)}
  130. ${#numbers.listFormatInteger(numList,3)}
  131. ${#numbers.setFormatInteger(numSet,3)}
  132. /*
  133. * Set minimum integer digits and thousands separator:
  134. * ‘POINT‘, ‘COMMA‘, ‘NONE‘ or ‘DEFAULT‘ (by locale).
  135. * Also works with arrays, lists or sets
  136. */
  137. ${#numbers.formatInteger(num,3,‘POINT‘)}
  138. ${#numbers.arrayFormatInteger(numArray,3,‘POINT‘)}
  139. ${#numbers.listFormatInteger(numList,3,‘POINT‘)}
  140. ${#numbers.setFormatInteger(numSet,3,‘POINT‘)}
  141. /*
  142. * ==========================
  143. * Formatting decimal numbers
  144. * ==========================
  145. */
  146. /*
  147. * Set minimum integer digits and (exact) decimal digits.
  148. * Also works with arrays, lists or sets
  149. */
  150. ${#numbers.formatDecimal(num,3,2)}
  151. ${#numbers.arrayFormatDecimal(numArray,3,2)}
  152. ${#numbers.listFormatDecimal(numList,3,2)}
  153. ${#numbers.setFormatDecimal(numSet,3,2)}
  154. /*
  155. * Set minimum integer digits and (exact) decimal digits, and also decimal separator.
  156. * Also works with arrays, lists or sets
  157. */
  158. ${#numbers.formatDecimal(num,3,2,‘COMMA‘)}
  159. ${#numbers.arrayFormatDecimal(numArray,3,2,‘COMMA‘)}
  160. ${#numbers.listFormatDecimal(numList,3,2,‘COMMA‘)}
  161. ${#numbers.setFormatDecimal(numSet,3,2,‘COMMA‘)}
  162. /*
  163. * Set minimum integer digits and (exact) decimal digits, and also thousands and
  164. * decimal separator.
  165. * Also works with arrays, lists or sets
  166. */
  167. ${#numbers.formatDecimal(num,3,‘POINT‘,2,‘COMMA‘)}
  168. ${#numbers.arrayFormatDecimal(numArray,3,‘POINT‘,2,‘COMMA‘)}
  169. ${#numbers.listFormatDecimal(numList,3,‘POINT‘,2,‘COMMA‘)}
  170. ${#numbers.setFormatDecimal(numSet,3,‘POINT‘,2,‘COMMA‘)}
  171. /*
  172. * ==========================
  173. * Utility methods
  174. * ==========================
  175. */
  176. /*
  177. * Create a sequence (array) of integer numbers going
  178. * from x to y
  179. */
  180. ${#numbers.sequence(from,to)}
  181. ${#numbers.sequence(from,to,step)}
  182. Strings
  183. #strings : utility methods for String objects:
  184. /*
  185. * ======================================================================
  186. * See javadoc API for class org.thymeleaf.expression.Strings
  187. * ======================================================================
  188. */
  189. /*
  190. * Check whether a String is empty (or null). Performs a trim() operation before check
  191. * Also works with arrays, lists or sets
  192. */
  193. ${#strings.isEmpty(name)}
  194. ${#strings.arrayIsEmpty(nameArr)}
  195. ${#strings.listIsEmpty(nameList)}
  196. ${#strings.setIsEmpty(nameSet)}
  197. /*
  198. * Perform an ‘isEmpty()‘ check on a string and return it if false, defaulting to
  199. * another specified string if true.
  200. * Also works with arrays, lists or sets
  201. */
  202. ${#strings.defaultString(text,default)}
  203. ${#strings.arrayDefaultString(textArr,default)}
  204. ${#strings.listDefaultString(textList,default)}
  205. ${#strings.setDefaultString(textSet,default)}
  206. /*
  207. * Check whether a fragment is contained in a String
  208. * Also works with arrays, lists or sets
  209. */
  210. ${#strings.contains(name,‘ez‘)} // also array*, list* and set*
  211. ${#strings.containsIgnoreCase(name,‘ez‘)} // also array*, list* and set*
  212. /*
  213. * Check whether a String starts or ends with a fragment
  214. * Also works with arrays, lists or sets
  215. */
  216. ${#strings.startsWith(name,‘Don‘)} // also array*, list* and set*
  217. ${#strings.endsWith(name,endingFragment)} // also array*, list* and set*
  218. /*
  219. * Substring-related operations
  220. * Also works with arrays, lists or sets
  221. */
  222. ${#strings.indexOf(name,frag)} // also array*, list* and set*
  223. ${#strings.substring(name,3,5)} // also array*, list* and set*
  224. ${#strings.substringAfter(name,prefix)} // also array*, list* and set*
  225. ${#strings.substringBefore(name,suffix)} // also array*, list* and set*
  226. ${#strings.replace(name,‘las‘,‘ler‘)} // also array*, list* and set*
  227. /*
  228. * Append and prepend
  229. * Also works with arrays, lists or sets
  230. */
  231. ${#strings.prepend(str,prefix)} // also array*, list* and set*
  232. ${#strings.append(str,suffix)} // also array*, list* and set*
  233. /*
  234. * Change case
  235. * Also works with arrays, lists or sets
  236. */
  237. ${#strings.toUpperCase(name)} // also array*, list* and set*
  238. ${#strings.toLowerCase(name)} // also array*, list* and set*
  239. /*
  240. * Split and join
  241. */
  242. ${#strings.arrayJoin(namesArray,‘,‘)}
  243. ${#strings.listJoin(namesList,‘,‘)}
  244. ${#strings.setJoin(namesSet,‘,‘)}
  245. ${#strings.arraySplit(namesStr,‘,‘)} // returns String[]
  246. ${#strings.listSplit(namesStr,‘,‘)} // returns List<String>
  247. ${#strings.setSplit(namesStr,‘,‘)} // returns Set<String>
  248. /*
  249. * Trim
  250. * Also works with arrays, lists or sets
  251. */
  252. ${#strings.trim(str)} // also array*, list* and set*
  253. /*
  254. * Compute length
  255. * Also works with arrays, lists or sets
  256. */
  257. ${#strings.length(str)} // also array*, list* and set*
  258. /*
  259. * Abbreviate text making it have a maximum size of n. If text is bigger, it
  260. * will be clipped and finished in "..."
  261. * Also works with arrays, lists or sets
  262. */
  263. ${#strings.abbreviate(str,10)} // also array*, list* and set*
  264. /*
  265. * Convert the first character to upper-case (and vice-versa)
  266. */
  267. ${#strings.capitalize(str)} // also array*, list* and set*
  268. ${#strings.unCapitalize(str)} // also array*, list* and set*
  269. /*
  270. * Convert the first character of every word to upper-case
  271. */
  272. ${#strings.capitalizeWords(str)} // also array*, list* and set*
  273. ${#strings.capitalizeWords(str,delimiters)} // also array*, list* and set*
  274. /*
  275. * Escape the string
  276. */
  277. ${#strings.escapeXml(str)} // also array*, list* and set*
  278. ${#strings.escapeJava(str)} // also array*, list* and set*
  279. ${#strings.escapeJavaScript(str)} // also array*, list* and set*
  280. ${#strings.unescapeJava(str)} // also array*, list* and set*
  281. ${#strings.unescapeJavaScript(str)} // also array*, list* and set*
  282. /*
  283. * Null-safe comparison and concatenation
  284. */
  285. ${#strings.equals(str)}
  286. ${#strings.equalsIgnoreCase(str)}
  287. ${#strings.concat(str)}
  288. ${#strings.concatReplaceNulls(str)}
  289. /*
  290. * Random
  291. */
  292. ${#strings.randomAlphanumeric(count)}
  293. Objects
  294. #objects : utility methods for objects in general
  295. /*
  296. * ======================================================================
  297. * See javadoc API for class org.thymeleaf.expression.Objects
  298. * ======================================================================
  299. */
  300. /*
  301. * Return obj if it is not null, and default otherwise
  302. * Also works with arrays, lists or sets
  303. */
  304. ${#objects.nullSafe(obj,default)}
  305. ${#objects.arrayNullSafe(objArray,default)}
  306. ${#objects.listNullSafe(objList,default)}
  307. ${#objects.setNullSafe(objSet,default)}
  308. Booleans
  309. #bools : utility methods for boolean evaluation
  310. /*
  311. * ======================================================================
  312. * See javadoc API for class org.thymeleaf.expression.Bools
  313. * ======================================================================
  314. */
  315. /*
  316. * Evaluate a condition in the same way that it would be evaluated in a th:if tag
  317. * (see conditional evaluation chapter afterwards).
  318. * Also works with arrays, lists or sets
  319. */
  320. ${#bools.isTrue(obj)}
  321. ${#bools.arrayIsTrue(objArray)}
  322. ${#bools.listIsTrue(objList)}
  323. ${#bools.setIsTrue(objSet)}
  324. /*
  325. * Evaluate with negation
  326. * Also works with arrays, lists or sets
  327. */
  328. ${#bools.isFalse(cond)}
  329. ${#bools.arrayIsFalse(condArray)}
  330. ${#bools.listIsFalse(condList)}
  331. ${#bools.setIsFalse(condSet)}
  332. /*
  333. * Evaluate and apply AND operator
  334. * Receive an array, a list or a set as parameter
  335. */
  336. ${#bools.arrayAnd(condArray)}
  337. ${#bools.listAnd(condList)}
  338. ${#bools.setAnd(condSet)}
  339. /*
  340. * Evaluate and apply OR operator
  341. * Receive an array, a list or a set as parameter
  342. */
  343. ${#bools.arrayOr(condArray)}
  344. ${#bools.listOr(condList)}
  345. ${#bools.setOr(condSet)}
  346. Arrays
  347. #arrays : utility methods for arrays
  348. /*
  349. * ======================================================================
  350. * See javadoc API for class org.thymeleaf.expression.Arrays
  351. * ======================================================================
  352. */
  353. /*
  354. * Converts to array, trying to infer array component class.
  355. * Note that if resulting array is empty, or if the elements
  356. * of the target object are not all of the same class,
  357. * this method will return Object[].
  358. */
  359. ${#arrays.toArray(object)}
  360. /*
  361. * Convert to arrays of the specified component class.
  362. */
  363. ${#arrays.toStringArray(object)}
  364. ${#arrays.toIntegerArray(object)}
  365. ${#arrays.toLongArray(object)}
  366. ${#arrays.toDoubleArray(object)}
  367. ${#arrays.toFloatArray(object)}
  368. ${#arrays.toBooleanArray(object)}
  369. /*
  370. * Compute length
  371. */
  372. ${#arrays.length(array)}
  373. /*
  374. * Check whether array is empty
  375. */
  376. ${#arrays.isEmpty(array)}
  377. /*
  378. * Check if element or elements are contained in array
  379. */
  380. ${#arrays.contains(array, element)}
  381. ${#arrays.containsAll(array, elements)}
  382. Lists
  383. #lists : utility methods for lists
  384. /*
  385. * ======================================================================
  386. * See javadoc API for class org.thymeleaf.expression.Lists
  387. * ======================================================================
  388. */
  389. /*
  390. * Converts to list
  391. */
  392. ${#lists.toList(object)}
  393. /*
  394. * Compute size
  395. */
  396. ${#lists.size(list)}
  397. /*
  398. * Check whether list is empty
  399. */
  400. ${#lists.isEmpty(list)}
  401. /*
  402. * Check if element or elements are contained in list
  403. */
  404. ${#lists.contains(list, element)}
  405. ${#lists.containsAll(list, elements)}
  406. /*
  407. * Sort a copy of the given list. The members of the list must implement
  408. * comparable or you must define a comparator.
  409. */
  410. ${#lists.sort(list)}
  411. ${#lists.sort(list, comparator)}
  412. Sets
  413. #sets : utility methods for sets
  414. /*
  415. * ======================================================================
  416. * See javadoc API for class org.thymeleaf.expression.Sets
  417. * ======================================================================
  418. */
  419. /*
  420. * Converts to set
  421. */
  422. ${#sets.toSet(object)}
  423. /*
  424. * Compute size
  425. */
  426. ${#sets.size(set)}
  427. /*
  428. * Check whether set is empty
  429. */
  430. ${#sets.isEmpty(set)}
  431. /*
  432. * Check if element or elements are contained in set
  433. */
  434. ${#sets.contains(set, element)}
  435. ${#sets.containsAll(set, elements)}
  436. Maps
  437. #maps : utility methods for maps
  438. /*
  439. * ======================================================================
  440. * See javadoc API for class org.thymeleaf.expression.Maps
  441. * ======================================================================
  442. */
  443. /*
  444. * Compute size
  445. */
  446. ${#maps.size(map)}
  447. /*
  448. * Check whether map is empty
  449. */
  450. ${#maps.isEmpty(map)}
  451. /*
  452. * Check if key/s or value/s are contained in maps
  453. */
  454. ${#maps.containsKey(map, key)}
  455. ${#maps.containsAllKeys(map, keys)}
  456. ${#maps.containsValue(map, value)}
  457. ${#maps.containsAllValues(map, value)}
  458. Aggregates
  459. #aggregates : utility methods for creating aggregates on arrays or collections
  460. /*
  461. * ======================================================================
  462. * See javadoc API for class org.thymeleaf.expression.Aggregates
  463. * ======================================================================
  464. */
  465. /*
  466. * Compute sum. Returns null if array or collection is empty
  467. */
  468. ${#aggregates.sum(array)}
  469. ${#aggregates.sum(collection)}
  470. /*
  471. * Compute average. Returns null if array or collection is empty
  472. */
  473. ${#aggregates.avg(array)}
  474. ${#aggregates.avg(collection)}
  475. Messages
  476. #messages : utility methods for obtaining externalized messages inside variables expressions, in the same way as they would be obtained using #{...} syntax.
  477. /*
  478. * ======================================================================
  479. * See javadoc API for class org.thymeleaf.expression.Messages
  480. * ======================================================================
  481. */
  482. /*
  483. * Obtain externalized messages. Can receive a single key, a key plus arguments,
  484. * or an array/list/set of keys (in which case it will return an array/list/set of
  485. * externalized messages).
  486. * If a message is not found, a default message (like ‘??msgKey??‘) is returned.
  487. */
  488. ${#messages.msg(‘msgKey‘)}
  489. ${#messages.msg(‘msgKey‘, param1)}
  490. ${#messages.msg(‘msgKey‘, param1, param2)}
  491. ${#messages.msg(‘msgKey‘, param1, param2, param3)}
  492. ${#messages.msgWithParams(‘msgKey‘, new Object[] {param1, param2, param3, param4})}
  493. ${#messages.arrayMsg(messageKeyArray)}
  494. ${#messages.listMsg(messageKeyList)}
  495. ${#messages.setMsg(messageKeySet)}
  496. /*
  497. * Obtain externalized messages or null. Null is returned instead of a default
  498. * message if a message for the specified key is not found.
  499. */
  500. ${#messages.msgOrNull(‘msgKey‘)}
  501. ${#messages.msgOrNull(‘msgKey‘, param1)}
  502. ${#messages.msgOrNull(‘msgKey‘, param1, param2)}
  503. ${#messages.msgOrNull(‘msgKey‘, param1, param2, param3)}
  504. ${#messages.msgOrNullWithParams(‘msgKey‘, new Object[] {param1, param2, param3, param4})}
  505. ${#messages.arrayMsgOrNull(messageKeyArray)}
  506. ${#messages.listMsgOrNull(messageKeyList)}
  507. ${#messages.setMsgOrNull(messageKeySet)}
  508. IDs
  509. #ids : utility methods for dealing with id attributes that might be repeated (for example, as a result of an iteration).
  510. /*
  511. * ======================================================================
  512. * See javadoc API for class org.thymeleaf.expression.Ids
  513. * ======================================================================
  514. */
  515. /*
  516. * Normally used in th:id attributes, for appending a counter to the id attribute value
  517. * so that it remains unique even when involved in an iteration process.
  518. */
  519. ${#ids.seq(‘someId‘)}
  520. /*
  521. * Normally used in th:for attributes in <label> tags, so that these labels can refer to Ids
  522. * generated by means if the #ids.seq(...) function.
  523. *
  524. * Depending on whether the <label> goes before or after the element with the #ids.seq(...)
  525. * function, the "next" (label goes before "seq") or the "prev" function (label goes after
  526. * "seq") function should be called.
  527. */
  528. ${#ids.next(‘someId‘)}
  529. ${#ids.prev(‘someId‘)}

六.thymeleaf的$和*的区别:

  1. <div th:object="${session.user}">
  2. <p>Name: <span th:text="*{firstName}">Sebastian</span>.</p>
  3. <p>Surname: <span th:text="*{lastName}">Pepper</span>.</p>
  4. <p>Nationality: <span th:text="*{nationality}">Saturn</span>.</p>
  5. </div>

这等同于如下方式:

  1. <div>
  2. <p>Name: <span th:text="${session.user.firstName}">Sebastian</span>.</p>
  3. <p>Surname: <span th:text="${session.user.lastName}">Pepper</span>.</p>
  4. <p>Nationality: <span th:text="${session.user.nationality}">Saturn</span>.</p>
  5. </div>

也可以混用,如下:

  1. <div th:object="${session.user}">
  2. <p>Name: <span th:text="*{firstName}">Sebastian</span>.</p>
  3. <p>Surname: <span th:text="${session.user.lastName}">Pepper</span>.</p>
  4. <p>Nationality: <span th:text="*{nationality}">Saturn</span>.</p>
  5. </div>

当一个对象的选择,选择的对象将也可用美元作为#对象表达变量表达式:

  1. <div th:object="${session.user}">
  2. <p>Name: <span th:text="${#object.firstName}">Sebastian</span>.</p>
  3. <p>Surname: <span th:text="${session.user.lastName}">Pepper</span>.</p>
  4. <p>Nationality: <span th:text="*{nationality}">Saturn</span>.</p>
  5. </div>

如果没有执行对象选择,美元和星号语法是完全等价的:

  1. <div>
  2. <p>Name: <span th:text="*{session.user.name}">Sebastian</span>.</p>
  3. <p>Surname: <span th:text="*{session.user.surname}">Pepper</span>.</p>
  4. <p>Nationality: <span th:text="*{session.user.nationality}">Saturn</span>.</p>
  5. </div>