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')";
}
相关推荐
本教程解释了Thymeleaf如何与Spring Framework集成,尤其是(但不仅仅是)Spring MVC。 根据官方文档全译中文版,需要的下载参考学习
本教程解释如何将Thymeleaf与Spring框架集成,特别是(但不仅是)Spring MVC。 为了充分理解本教程,您应该首先理解深入解释Thymeleaf标准方言的“使用Thymeleaf”教程。
thymeleaf,我个人认为是个比较好的模板,性能也比一般的,比如freemaker的要高,而且把将美工和程序员能够结合起来,美工能够在浏览器中查看静态效果,程序员可以在应用服务器查看带数据的效果。 thymeleaf是一个...
博客里有对应入门程序,可用于参考
整合Thymeleaf+Spring的官网英文教程,针对Spring3和Spring4。
JAVA后台管理系统-集成了Thymeleaf、Spring MVC、Shiro、MyBatis、quartz
本系统是基于Thymeleaf+SpringBoot+SpringDataJPA实现的的中小医院信息管理系统。简单实现了挂号收费,门诊管理,划价收费,药房取药,体检管理,药房管理,系统维护等基础功能。就诊卡提供了手动和读卡两种方式录入...
Thymeleaf拥有适用于Spring Framework的模块,与您喜欢的工具的大量集成以及插入您自己的功能的能力,对于现代HTML5 JVM Web开发而言,Thymeleaf是理想的选择-尽管它还有很多工作要做。 要注意的是thymeleaf不是...
spring boot 、mybatis、spring security整合,使用thymeleaf模版引擎
使用springmvc、spring security、bootstrap、thymeleaf开发的具有CRUD功能的后台管理。
spring boot thymeleaf 整合代码
Thymeleaf+Spring 测试项目该项目的目的是研究 Spring 和 Thymeleaf 作为 Eionet 软件的新软件标准。 设置为E-PRTR的demo,供大家讨论改写使用。 该应用程序使用 Liquibase 创建和升级数据库,并使用 Thymeleaf 作为...
包含attoparser-2.0.4.RELEASE.jar,thymeleaf-spring3-3.0.9.RELEASE.jar,thymeleaf-spring4-3.0.9.RELEASE.jar,unbescape-1.1.5.RELEASE.jar
- Spring Boot AJAX 跨域,包括 JSONP、Node.js与SpringBoot集成使用反向代理 等。 - springboot-websockets - Spring Boot 使用 Websocket - springboot-webflux - Spring Boot 集成 WebFlux 开发反应式 Web...
该存储库包含3个项目: thymeleaf-extras-springsecurity3与Spring Security 3.x集成thymeleaf-extras-springsecurity4用于与Spring Security 4.x集成thymeleaf-extras-springsecurity5与Spring Security 5.x集成...
thymeleaf-spring:用于Spring的Thymeleaf集成模块
Spring Security整合Thymeleaf实现访问控制,开发工具:IDEA
springboot整合thymeleaf模板
springboot+thymeleaf+maven整和的一个小案例,实现异常跳转页面
Spring boot整合mybaties+thymeleaf实现基础,实现thymeleaf页面的替换