<div id="cnblogs_post_body" class="blogpost-body"><p> 1、引用命名空间 <html xmlns:th="http://www.thymeleaf.org"> </p> <p> 在html中引入此命名空间,可避免编辑器出现html验证错误,虽然加不加命名空间对Thymeleaf的功能没有任何影响。</p> <p> </p> <p>2、输出内容</p> <p> 2.1 <p th:text="#{home.welcome}">Welcome to our grocery store!</p></p> <p> 说明:</p> <p> 1. th:text 用来将内容输出到所在标签的body中。</p> <p> 2. #{home.welcome} 用来引入数据home对象中的 welcome属性。</p> <p> 3. 能够用th:utext 用来显示“unescaped ” 的html内容。</p> <p> 2.2 <p>Today is: <span th:text="${today}">13 February 2011</span></p></p> <p> 说明:${today} 用来引用 today 变量</p> <p>3、访问对象 </p> <p> ${param.x} 返回名为x 的 request参数。(可能有多个值)</p> <p> ${session.x} 返回名为x的Session参数。</p> <p> ${application.x} 返回名为 servlet context 的参数。</p> <p> </p> <p>4、基本语法</p> <p> 4.1 #{home.welcome} -- 访问数据</p> <p> 4.2 #{home.welcome(${session.user.name})} -- 格式化数据 当 home.welcome 为 "abcdegf{0}" 相似这种内容时。(多个参数以逗句分隔)。</p> <p> 4.3 ${today} --- 访问变量</p> <p> 4.4 访问基本对象</p> <p style="margin-left: 30px">#ctx: the context object.<br>#vars: the context variables.<br>#locale: the context locale.<br>#request: (only in Web Contexts) the HttpServletRequest object.<br>#response: (only in Web Contexts) the HttpServletResponse object.<br>#session: (only in Web Contexts) the HttpSession object.<br>#servletContext: (only in Web Contexts) the ServletContext object.</p> <p style="margin-left: 30px">其它公共对象参考: http://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#appendix-a-expression-basic-objects</p> <p> 4.5 日期的输出</p> <p> <span th:text="${#calendars.format(today,'dd MMMM yyyy')}">13 May 2011</span></p> <p> 4.6 星号语法</p> <p style="margin-left: 30px"> <div th:object="${session.user}"><br> <p>Name: <span th:text="*{firstName}">Sebastian</span>.</p><br> <p>Surname: <span th:text="*{lastName}">Pepper</span>.</p><br> <p>Nationality: <span th:text="*{nationality}">Saturn</span>.</p><br> </div></p> <p style="margin-left: 30px">4.7 输出URL</p> <p> <a href="product/list.html" th:href="@{/product/list}">Product List</a></p> <p> <a href="details.html" th:href="@{/order/{orderId}/details(orderId=${o.id})}">view</a></p> <p> 4.8 使用代码段</p> <p> <div th:insert="~{commons :: main}">...</div></p> <p> 4.9 直接输出内容 </p> <p style="margin-left: 30px"><span th:text="'working web application'"> -- 输出字符</p> <p style="margin-left: 30px"><span th:text="2013 + 2"> -- 输出数据表达式</p> <p style="margin-left: 30px"><div th:if="${user.isAdmin()} == false"> --输出布尔表达式</p> <p style="margin-left: 30px"><span th:text="'Welcome to our application, ' + ${user.name} + '!'"> -- 带变量的</p> <p style="margin-left: 30px">4.10 条件表达式</p> <p style="margin-left: 30px"><tr th:class="${row.even}? 'even' : 'odd'"><br> ... <br></tr></p> <p style="margin-left: 30px"><tr th:class="${row.even}? 'alt'"><br> ...省略 false 结果的表达方式<br></tr></p> <p style="margin-left: 30px"><div th:object="${session.user}"><br> ...省略 true 结果的表达方式<br> <p>Age: <span th:text="*{age}?: '(no age specified)'">27</span>.</p><br></div></p> <p style="margin-left: 30px"><span th:text="${user.name} ?: _">no user authenticated</span> --不作任何处理时用下划线 _ 表示</p> <p style="margin-left: 30px">4.11 格式化 </p> <p> <td th:text="${{user.lastAccessDate}}">...</td> --${{.}} 调用默认的格式化器来输出结果。</p> <p> 4.12 预处理</p> <p> <p th:text="${__#{article.text('textVar')}__}">Some text here...</p> </p> <p> 说明:thymeleaf 的处理模板内容的顺序与书写顺序无关,只能经过 __${expression}__ ,来将须要先一步计算出来后面 要用的变量指定为优化处理。</p> <p> </p> <p> 5、设置 Attribute 值</p> <p> 5.1 设置任何Attribute 的方法</p> <p> <input type="submit" value="Subscribe!" th:attr="value=#{subscribe.submit}"/> --设置单个</p> <p> <img src="../../images/gtvglogo.png" th:attr="src=@{/images/gtvglogo.png},title=#{logo},alt=#{logo}" /> --一次设置多个</p> <p> 5.2 设置一些内置的Attribute的方法 </p> <p> <li><a href="product/list.html" th:href="@{/product/list}">Product List</a></li></p> <p> <form action="subscribe.html" th:action="@{/subscribe}"></p> <p> <input type="submit" value="Subscribe!" th:value="#{subscribe.submit}"/></p> <p> <img src="../../images/gtvglogo.png" th:src="@{/images/gtvglogo.png}" th:alt-title="#{logo}" /> -- 一次设置多个(alt title)的方法</p> <p> 其它的可用属性:http://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#setting-value-to-specific-attributes</p> <p> 5.3 设置html里没有指的任何属性的语法</p> <p> <span th:whatever="${user.name}">...</span> ---whatever 能够换成任何你想设的属性</p> <p> </p> <p>6、循环输出的语法</p> <p> 6.1 基本循环</p> <p style="margin-left: 30px"><tr th:each="prod : ${prods}"></p> <p style="margin-left: 30px"> <td th:text="${prod.name}">Onions</td><br> <td th:text="${prod.price}">2.41</td><br> <td th:text="${prod.inStock}? #{true} : #{false}">yes</td><br> </tr></p> <p style="margin-left: 30px">6.2 循环状态的使用</p> <p style="margin-left: 30px"><table><br> <tr><br> <th>NAME</th><br> <th>PRICE</th><br> <th>IN STOCK</th><br> </tr><br> <tr th:each="prod,iterStat : ${prods}" th:class="${iterStat.odd}? 'odd'"><br> <td th:text="${prod.name}">Onions</td><br> <td th:text="${prod.price}">2.41</td><br> <td th:text="${prod.inStock}? #{true} : #{false}">yes</td><br> </tr><br></table></p> <p> 关于状态的其它信息的使用详细参考:http://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#keeping-iteration-status</p> <p> </p> <p>7、条件判断</p> <p> 7.1 if 和 unless</p> <p> <a href="comments.html" th:href="@{/comments(prodId=${prod.id})}" th:unless="${#lists.isEmpty(prod.comments)}">view</a></p> <p> <a href="comments.html" th:href="@{/product/comments(prodId=${prod.id})}" th:if="${not #lists.isEmpty(prod.comments)}">view</a></p> <p> 7.2 switch 语句</p> <p style="margin-left: 30px"><div th:switch="${user.role}"><br> <p th:case="'admin'">User is an administrator</p><br> <p th:case="#{roles.manager}">User is a manager</p><br> <p th:case="*">User is some other thing</p> --默认的 case 至关于default<br></div></p> <p> </p> <p>8、模板 include</p> <p> 8.1 定义和引用代码块</p> <p> 定义代码块</p> <p style="margin-left: 30px"><!DOCTYPE html></p> <p style="margin-left: 30px"><html xmlns:th="http://www.thymeleaf.org"></p> <p style="margin-left: 30px"> <body><br> <br> <div th:fragment="copy"><br> &copy; 2011 The Good Thymes Virtual Grocery<br> </div><br> <br> </body><br> <br></html></p> <p style="margin-left: 30px">引用代码块</p> <p style="margin-left: 30px"><body></p> <p style="margin-left: 30px"> ...</p> <p style="margin-left: 30px"> <div th:insert="~{footer :: copy}"></div><br> <br></body></p> <p style="margin-left: 30px">引用未用fragment 标注的代码块 </p> <p style="margin-left: 30px"><div id="copy-section"><br> &copy; 2011 The Good Thymes Virtual Grocery<br></div></p> <p style="margin-left: 30px"><body></p> <p style="margin-left: 30px"> ...</p> <p style="margin-left: 30px"> <div th:insert="~{footer :: #copy-section}"></div><br> <br></body></p> <p style="margin-left: 30px">8.2 th:insert th:replace th:include 之间的区别</p> <p style="margin-left: 30px">th:insert --- 插入代码块 th:replace -- 替换代码块会替换掉容器标签 th:include ---- 和insert类似但只会插入fragment标注body内的内容。</p> <p style="margin-left: 30px">8.3 带参数的代码段</p> <p style="margin-left: 30px"><div th:fragment="frag (onevar,twovar)"><br> <p th:text="${onevar} + ' - ' + ${twovar}">...</p><br></div></p> <p> <div th:replace="::frag (${value1},${value2})">...</div><br> <div th:replace="::frag (onevar=${value1},twovar=${value2})">...</div></p> <p> </p> <p>9、局部变量的使用示例</p> <p style="margin-left: 30px"><div th:with="firstPer=${persons[0]}"><br> <p><br> The name of the first person is <span th:text="${firstPer.name}">Julius Caesar</span>.<br> </p><br></div></p> <p style="margin-left: 30px"><div th:with="firstPer=${persons[0]},secondPer=${persons[1]}"><br> <p><br> The name of the first person is <span th:text="${firstPer.name}">Julius Caesar</span>.<br> </p><br> <p><br> But the name of the second person is <br> <span th:text="${secondPer.name}">Marcus Antonius</span>.<br> </p><br></div></p> <p>10、注释</p> <p> <code><!-- ... --> </code></p> <p>11、说明</p> <p> 以上只列出Thymeleaf了简要经常使用的语法和使用方式,更多详情的说明和规则请参见:http://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#introducing-thymeleaf</p></div>html