Thymeleaf 基本用法总结

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

相关文章
相关标签/搜索