thymeleaf 语法

14 篇文章 0 订阅
1 篇文章 0 订阅
1.Thymeleaf是什么

       Thymeleaf是面向Web和独立环境的现代服务器端Java模板引擎, 能够处理HTML, XML, JavaScript, CSS甚至是纯文本。

2.标准表达式语法:
2.1 简单表达式
  • 变量表达式:${}
<span th:text="${user.name}">
  • 选择变量表达式:*{}
//它们所作用的对象由th:object属性指定:

<div th:object="${user}">
  ...
  <span th:text="*{name}">...</span>
  ...
</div>
  • 消息表达式:#{}
<th th:text="#{user.name}">...</th>
  • URL表达式:@{}
<a href="detail.html" th:href="@{http://localhost:8080/user/detail(userId=${user.userId})}">view</a>
<a th:href="@{/user/list}">...</a>
<a href="/user/list">...</a>
  • 片段表达式:~{}
<div th:with="frag=~{footer :: #main/text()}">
  <p th:insert="${frag}">
</div>
2.2 文本和操作
  1. 文字

    文本文字:'one text' , 'Another one!' ,…

    数字文字:0 , 34 , 3.0 , 12.3 ,…

    布尔文字: true , false

    空字面值: null

    文字标记: one , sometext , main ,…

  2. 文本操作:

    字符串连接: +
    文字替换: |The name is ${name}|

  3. 算数运算::

    二进制操作: + , - , * , / , %
    负号 (一元操作符):-

  4. 布尔运算:

    二进制操作符: and , or
    布尔否定 (unary operator): ! , not

  5. 比较运算符:

    比较运算符:> , < , >= , <= ( gt , lt , ge , le )
    相等运算符: == , != ( eq , ne)

  6. 条件操作:

    If-then: (if) ? (then)
    If-then-else: (if) ? (then) : (else)

  7. 特殊符号
    哑操作符: _

  8. 预处理
    _ ${expression} _

3.其他操作
  1. 设置属性值
原始表单

<form action="subscribe.html">
    <fieldset>
        <input type="text" name="email" />
        <input type="submit" value="Subscribe me!" />
    </fieldset>
</form>

我们可以通过th:attr来设置input或者form或者其他标签的属性。如:

<form action="subscribe.html" th:attr="action=@{/subscribe}">
    <fieldset>
        <input type="text" name="email" />
        <input type="submit" value="Subscribe me!" th:attr="value=#{subscribe.submit}"/>
    </fieldset>
</form>

他会替换相同属性的值。结果如下:

<form action="/gtvg/subscribe">
    <fieldset>
        <input type="text" name="email" />
        <input type="submit" value="¡Suscríbeme!"/>
    </fieldset>
</form>

2.循环

<tr th:each="user,iterStat : ${userList}" th:class="${iterStat.odd}? 'odd'">
<td th:text="${user.name}">helen</td>
<td th:text="${user.phone}">18163912111</td>
<td th:text="${user.age}">18</td>
<td th:text="${user.type}">进店</td>
<td th:text="${user.status}? #{true} : #{false}">yes</td>
</tr>
    iterStat称作状态变量,属性有:

    index:当前迭代对象的index(从0开始计算)
    count: 当前迭代对象的index(从1开始计算)
    size:被迭代对象的大小
    current:当前迭代变量
    even/odd:布尔值,当前循环是否是偶数/奇数(从0开始计算)
    first:布尔值,当前循环是否是第一个
    last:布尔值,当前循环是否是最后一个

3.条件判断

<td><span th:if="${user.age lt 18}">少年</span></td>
<a th:unless=${session.user != null} th:href="@{/login}" >Login</a>//条件不成立显示


<td th:switch="${user.type}">
    <span th:case="'JUST PASSING'">路过进店</span>
    <span th:case="'WITH FRIENDS'">朋友介绍</span>
    <span th:case="'LEAFLETS'">传单</span>
    <span th:case="*">其它来源</span>
</td>

4.内联
[[…]]或[(…)]中的表达式被认为是在Thymeleaf中内联的表达式, 任何在 th:text或th:utext属性中使⽤的表达式都可以出现在[[]]或[()]中。

<p>Hello, [[${user.name}]]!</p>

注意, [[…]]等价于th:text(即结果将被HTML转义), [(…)]等价于
th:utext, 不会执行任何HTML转义。

msg ='This is<b>great!</b>'
<p>The message is "[(${msg})]"</p>
<p>The message is "This is <b>great!</b>"</p>

<p>The message is "[[${msg}]]"</p>
<p>The message is "This is &lt;b&gt;great!&lt;/b&gt;"</p>//转义
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值