thymeleaf html5

一、引用命名空间 <html xmlns:th="http://www.thymeleaf.org"> 

        在html中引入此命名空间,可避免编辑器出现html验证错误,虽然加不加命名空间对Thymeleaf的功能没有任何影响。

二. 取值

  #{home.welcome} --  访问数据

       #{home.welcome(${session.user.name})}  -- 格式化数据 当 home.welcome 为 "abcdegf{0}"  类似这种内容时。(多个参数以逗句分隔)。

  ${today} --- 访问变量

   日期的输出 <span th:text="${#calendars.format(today,'dd MMMM yyyy')}">13 May 2011</span>

三、访问对象      

       ${param.x} 返回名为x 的 request参数。(可能有多个值)

       ${session.x} 返回名为x的Session参数。

       ${application.x} 返回名为 servlet context 的参数 

  星号语法

  <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>

  输出URL

       <a href="product/list.html" th:href="@{/product/list}">Product List</a>

       <a href="details.html" th:href="@{/order/{orderId}/details(orderId=${o.id})}">view</a>

  直接输出内容   

  <span th:text="'working web application'"> -- 输出字符

  <span th:text="2013 + 2">  -- 输出数据表达式

  <div th:if="${user.isAdmin()} == false">  --输出布尔表达式

  <span th:text="'Welcome to our application, ' + ${user.name} + '!'"> -- 带变量的

  条件表达式

  <tr th:class="${row.even}? 'even' : 'odd'">
  ...  
  </tr>

循环输出的语法

       6.1 基本循环

<tr th:each="prod : ${prods}">

     <td th:text="${prod.name}">Onions</td>
     <td th:text="${prod.price}">2.41</td>
     <td th:text="${prod.inStock}? #{true} : #{false}">yes</td>
</tr>

循环状态的使用

  <table>
  <tr>
  <th>NAME</th>
  <th>PRICE</th>
  <th>IN STOCK</th>
  </tr>
  <tr th:each="prod,iterStat : ${prods}" th:class="${iterStat.odd}? 'odd'">
  <td th:text="${prod.name}">Onions</td>
  <td th:text="${prod.price}">2.41</td>
  <td th:text="${prod.inStock}? #{true} : #{false}">yes</td>
  </tr>
  </table>

       关于状态的其它信息的使用详细参考:http://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#keeping-iteration-status

  更多详情的说明和规则请参见:http://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#introducing-thymeleaf

原文地址:https://www.cnblogs.com/ch94/p/10030514.html