thymeleaf学习

本文介绍Thymeleaf模板引擎的基本用法,包括变量表示语法、字符串拼接及各种表达式操作符等,并列举了常用的功能函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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).
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值