1、使用的是Spring EL而不是Ognl。
2、訪問上下文的Bean用${@myBean.doSomething()}
3、th:field,th:errors,th:errorclass用於form processing。
4、要采用SpringTemplateEngine。
5、基本配置:
class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<property name="prefix" value="/WEB-INF/templates/" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML5" />
</bean>
<bean id="templateEngine"
class="org.thymeleaf.spring3.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver" />
</bean>
<property name="templateEngine" ref="templateEngine" />
<property name="order" value="1" />
<property name="viewNames" value="*.html,*.xhtml" />
</bean>
6、被@ModelAttribute注釋的方法會在此controller每個方法執行前被執行,如果@ModelAttribute注釋的方法有返回值,則表示在model中存放隱含名稱的屬性對象,比如返回Account,則相當於model.addAttribute("account",account),如果@ModelAttribute(value="aname")注釋方法,則表示在model中增加aname的屬性值。@ModelAttribute注釋一個方法的參數則表示從model中或者從Form表單或者url中獲取。
7、 @RequestMapping("/hello")public void novoid() { },返回視圖為前綴+/hello+后綴,當方法返回Map,ModelMap等時都是相當於Request.setAttribute()。
8、<td th:text="${{sb.datePlanted}}">13/01/2011</td>雙括號表示自動使用轉換,常用於格式轉換。
9、<td th:text="${#strings.arrayJoin(#messages.arrayMsg(#strings.arrayPrepend(sb.features,'seedstarter.feature.')),', ')}">Electric Heating, Turf</td>,首先將數組feathers都加上前綴,然后利用messages翻譯國際化,最終組合成一個字符串。
10、用th:object指定command object,比如:<form action="#" th:action="@{/save}" th:object="${person}" method="post">,兩點限制,第一object只能是model 的直接attribute,不能使${person.baseInfo},第二,th:object的子級標簽內不能再使用th:object。inputField使用:<input type="text" th:field="*{datePlanted}" />。
12、checkbox標簽:
<label th:for="${#ids.next('covered')}" th:text="#{seedstarter.covered}">Covered</label>
<input type="checkbox" th:field="*{covered}" />
</div>
checkbox array:
<li th:each="feat : ${allFeatures}">
<input type="checkbox" th:field="*{features}" th:value="${feat}" />
<label th:for="${#ids.prev('features')}" th:text="#{${'seedstarter.feature.' + feat}}">Heating</label>
</li>
</ul>
13、radios:
<li th:each="ty : ${allTypes}">
<input type="radio" th:field="*{type}" th:value="${ty}" />
<label th:for="${#ids.prev('type')}" th:text="#{${'seedstarter.type.' + ty}}">Wireframe</label>
</li>
</ul>
14、dropdownlist or select。
<option th:each="type : ${allTypes}"
th:value="${type}"
th:text="#{${'seedstarter.type.' + type}}">Wireframe</option>
</select>
15、預處理:<select th:field="*{rows[__${rowStat.index}__].variety}">而不使用<select th:field="*{rows[rowStat.index].variety}">,因為spring el不會計算數組索引中的變量或者表達式。
16、錯誤顯示:<input type="text" th:field="*{datePlanted}" th:class="${#fields.hasErrors('datePlanted')}? fieldError" />
<li th:each="err : ${#fields.errors('datePlanted')}" th:text="${err}" />
</ul>
<p th:if="${#fields.hasErrors('datePlanted')}" th:errors="*{datePlanted}">Incorrect date</p>
<li th:each="err : ${#fields.errors('*')}" th:text="${err}">Input is incorrect</li>
</ul>
<li th:each="err : ${#fields.errors('global')}" th:text="${err}">Input is incorrect</li>
</ul>
<div th:errors="${myForm.date}">...</div>
<div th:errors="${myForm.*}">...</div>
<div th:if="${#fields.hasErrors('${myForm}')}">...</div>
<div th:if="${#fields.hasErrors('${myForm.date}')}">...</div>
<div th:if="${#fields.hasErrors('${myForm.*}')}">...</div>
<form th:object="${myForm}">
...
</form>
18、渲染模板的片段,常用於ajax,返回一部分文本做替換使用。
在ViewBean中指定片段:
<property name="templateName" value="index" />
<property name="fragmentSpec">
<bean class="org.thymeleaf.standard.fragment.StandardDOMSelectorFragmentSpec"
c:selectorExpression="content" />
</property>
</bean>
public String showContentPart() {
...
return "content-part";//返回上面定義的bean名稱。
}
在controller中指定片段:
public String showContentPart() {
...
return "index :: content";
}
還可以返回帶參數的片段:
public String showContentPart() {
...
return "index :: #content ('myvalue')";
}