thymeleaf学习
thymeleaf为java web开发中的一种模板视图组件。
变量
变量表示语法:
- Variable Expressions: ${…}
- Selection Variable Expressions: *{…}
- Message Expressions: #{…}
- Link URL Expressions: @{…}
<input type="text" name="userName" value="James Carrot" th:value="${user.name}" />
/*
* 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('-')}
字符串拼接简单写法
<span th:text="|Welcome to our application, ${user.name}!|">
<span th:text="'Welcome to our application, ' + ${user.name} + '!'">
*
表达式有点类似js中的with表达式
<div th:object="${session.user}">
<p>Name: <span th:text="*{firstName}">Sebastian</span>.</p>
<p>Surname: <span th:text="*{lastName}">Pepper</span>.</p>
<p>Nationality: <span th:text="*{nationality}">Saturn</span>.</p>
</div>
类型
- Text literals: ‘one text’, ‘Another one!’,…
- Number literals: 0, 34, 3.0, 12.3,…
- Boolean literals: true, false
- Null literal: null
- Literal tokens: one, sometext, main,…
Text operations:
- String concatenation: +
- Literal substitutions: |The name is ${name}|
Arithmetic operations:
- Binary operators: +, -, *, /, %
- Minus sign (unary operator): -
Boolean operations:
- Binary operators: and, or
- Boolean negation (unary operator): !, not
Comparisons and equality:
- Comparators: >, <, >=, <= (gt, lt, ge, le)
- Equality operators: ==, != (eq, ne)
Conditional operators:
- If-then: (if) ? (then)
- If-then-else: (if) ? (then) : (else)
- Default: (value) ?: (defaultvalue)
常用函数
- #dates: utility methods for java.util.Date objects: formatting, component extraction, etc.
- #calendars: analogous to #dates, but for java.util.Calendar objects.
- #numbers: utility methods for formatting numeric objects.
- #strings: utility methods for String objects: contains, startsWith, prepending/appending, etc.
- #objects: utility methods for objects in general.
- #bools: utility methods for boolean evaluation.
- #arrays: utility methods for arrays.
- #lists: utility methods for lists.
- #sets: utility methods for sets.
- #maps: utility methods for maps.
- #aggregates: utility methods for creating aggregates on arrays or collections.
- #messages: utility methods for obtaining externalized messages inside variables expressions, in the same way as they would be obtained using {…} syntax.
- #ids: utility methods for dealing with id attributes that might be repeated (for example, as a result of an iteration).