下面以构造一个表单开始,讲解 Thymeleaf的用法。为了演示方便,还是以经典的注册为例。
这是Thymeleaf的form的形式,
<form action="#" th:action="@{/register}" th:object="${person}" method="post"> </form>
首先是绝对路径,
<a th:href="@{http://www.baidu.com}">
<a th:href="@{/login}">解析后就是,
<a href="/myapp/login">
<a th:href="@{~/other-app/hello.html}" >
<a href="/other-app/showDetails.htm">
<a th:href="@{<span style="font-family: Arial, Helvetica, sans-serif;">//code.jquery.com/jquery-1.10.2.js</span><span style="font-family: Arial, Helvetica, sans-serif;">}" ></span>
<a href="//code.jquery.com/jquery-1.10.2.js">
接下来是URL的添加参数问题,
<a th:href="@{/order/details(id=3)}">
<a href="/order/details?id=3">
下面的形式也是支持的,请细细体会,
<a th:href="@{/order/{id}/details(id=3,action='show_all')}">解析后,
<a href="/order/3/details?action=show_all">还有一个概念,叫URL fragment,什么是URL Fragment呢?
上面两张图基本说明了url fragment是什么了。
<a th:href="@{/home#all_info(action='show')}">
<a href="/home?action=show#all_info">
下一个知识点是th:object,
该属性在Thymeleaf很常见,但form会强制要求你写这个。为了整合Spring,它对form里面的th:object规定如下:
@RequestMapping(value = "/processForm", method=RequestMethod.POST) public String processForm(@ModelAttribute(value="foo") Foo foo) { ... }
<form action="#" th:action="@{/processForm}" th:object="${foo}" method="post"> <input type="text" th:field="*{bar}" /> <input type="submit" /> </form>
public class Foo { private String bar; public String getBar() { return bar; } public void setBar(String bar) { this.bar = bar; } }
<span th:text="${book.author.name}">
<div th:object="${book}"> ... <span th:text="*{title}">...</span> ... </div>前面选择了book,接下来就按照它求值。
<table> ... <th th:text="#{header.address.city}">...</th> <th th:text="#{header.address.country}">...</th> ... </table>
Spring MVC : Java模板引擎 Thymeleaf (三)
原文地址:http://blog.csdn.net/chenloveit/article/details/38910803