`

thymeleaf与spring整合

阅读更多

 

 

1、使用的是Spring EL而不是Ognl。
2、访问上下文的Bean用${@myBean.doSomething()}
3、th:field,th:errors,th:errorclass用于form processing。
4、要采用SpringTemplateEngine。
5、基本配置:

<bean id="templateResolver"
       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>

<bean class="org.thymeleaf.spring3.view.ThymeleafViewResolver">
  <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标签:

<div>
  <label th:for="${#ids.next('covered')}" th:text="#{seedstarter.covered}">Covered</label>
  <input type="checkbox" th:field="*{covered}" />
</div>
checkbox array:
<ul>
  <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:
<ul>
  <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。
<select th:field="*{type}">
  <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" />
<ul>
  <li th:each="err : ${#fields.errors('datePlanted')}" th:text="${err}" />
</ul>

<input type="text" th:field="*{datePlanted}" />
<p th:if="${#fields.hasErrors('datePlanted')}" th:errors="*{datePlanted}">Incorrect date</p>

<input type="text" th:field="*{datePlanted}" class="small" th:errorclass="fieldError" />

<ul th:if="${#fields.hasErrors('*')}">
  <li th:each="err : ${#fields.errors('*')}" th:text="${err}">Input is incorrect</li>
</ul>
全局错误:
<ul th:if="${#fields.hasErrors('global')}">
  <li th:each="err : ${#fields.errors('global')}" th:text="${err}">Input is incorrect</li>
</ul>
在form外显示错误:
<div th:errors="${myForm}">...</div>
<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>
17、利用功能类转换:#conversions.convert(Object,Class),#conversions.convert(Object,String)
18、渲染模板的片段,常用于ajax,返回一部分文本做替换使用。
在ViewBean中指定片段:
<bean name="content-part" class="org.thymeleaf.spring3.view.ThymeleafView">
  <property name="templateName" value="index" />
  <property name="fragmentSpec">
    <bean class="org.thymeleaf.standard.fragment.StandardDOMSelectorFragmentSpec"
          c:selectorExpression="content" /> 
  </property>
</bean>

@RequestMapping("/showContentPart")
public String showContentPart() {
    ...
    return "content-part";//返回上面定义的bean名称。
}
c:selectorExpression="content":需要在content节点加上th:fragment。
c:selectorExpression="#content" :完全基于html dom selector,无需th:fragment。
在controller中指定片段:
@RequestMapping("/showContentPart")
public String showContentPart() {
    ...
    return "index :: content";
}
"index :: content"和"index ::#content"区别一样。
还可以返回带参数的片段:
@RequestMapping("/showContentPart")
public String showContentPart() {
    ...
    return "index :: #content ('myvalue')";
}
 
 
转自:http://www.blogjava.net/bjwulin/archive/2014/02/11/409734.html
分享到:
评论

相关推荐

    Thymeleaf与Spring集成

    Thymeleaf与Spring集成的关键点在于,它能无缝地与Spring MVC结合,提供了对Spring表达式语言(Spring Expression Language,SpEL)、国际化、资源管理、表单处理等Spring核心功能的支持。通过集成,Thymeleaf可以像...

    thymeleaf for spring pdf

    Thymeleaf与Spring框架的集成是本文的重要知识点。Thymeleaf提供了对Spring 3.x和4.x版本的支持,通过名为thymeleaf-spring3和thymeleaf-spring4的两个独立库实现。这些库被打包为单独的.jar文件(thymeleaf-spring3...

    Thymeleaf+spring 参考文档

    ### Thymeleaf与Spring框架集成详解 #### 引言 Thymeleaf是一种现代、高性能的XML/XHTML/HTML5模板引擎,适用于Web和非Web环境中的多种应用。它支持静态或动态内容生成,特别适合与Spring框架结合使用。本文章基于...

    thymeleaf整合spring所需包

    `thymeleaf-spring4-3.0.3.jar`是Thymeleaf与Spring框架集成的核心库,适用于Spring 4.x版本。这个版本的Thymeleaf支持Spring的注解驱动开发,如@Controller、@RequestMapping等,并且能与Spring的模型视图解析器...

    Thymeleaf+Spring官网教程

    本教程主要介绍如何将Thymeleaf模板引擎与Spring框架进行整合,并特别关注于Spring MVC应用中的集成方式。Thymeleaf是一种现代服务器端的Java模板引擎,它可以提供HTML原型(即XHTML/XML)的渲染,同时也支持文本、...

    Thymeleaf + Spring-3.0.11中文参考手册.zip

    本教程解释如何将Thymeleaf与Spring框架集成,特别是(但不仅是)Spring MVC。 为了充分理解本教程,您应该首先理解深入解释Thymeleaf标准方言的“使用Thymeleaf”教程。

    spring mvc整合thymeleaf示例

    thymeleaf,我个人认为是个比较好的模板,性能也比一般的,比如freemaker的要高,而且把将美工和程序员能够结合起来,美工能够在浏览器中查看静态效果,程序员可以在应用服务器查看带数据的效果。 thymeleaf是一个...

    thymeleaf_spring

    ### Thymeleaf与Spring框架集成教程 #### 引言 本教程旨在详细介绍如何将Thymeleaf与Spring框架(特别是Spring MVC)进行集成。通过本教程的学习,开发者可以了解到如何利用Thymeleaf作为Spring MVC应用中的视图...

    Spring3+ThymeLeaf

    接下来,我们讨论如何将Thymeleaf与Spring集成以发送邮件。在Spring框架中,我们可以使用JavaMailSender接口来发送邮件。首先,需要配置JavaMailSender,包括SMTP服务器的地址、端口、用户名和密码等信息。然后,...

    thymeleaf3.05.jar以及thymeleaf-spring3.jar

    总结来说,Thymeleaf 3.0.5及其Spring适配器3.0.5.RELEASE提供了强大的模板引擎功能,使开发者能够在HTML中编写整洁、可读性强的代码,同时保持与Spring的紧密集成。这两个JAR文件的使用,能极大地提升Web应用的开发...

    thymeleafspring.pdf

    Thymeleaf与Spring的集成,为开发者提供了一个灵活、功能强大的模板引擎,能够优雅地与Spring生态系统的其他部分(如Spring MVC、Spring表达式语言和Spring的消息源)协作,为Java Web应用提供高效、直观的开发体验...

    thymeleaf相关文档

    - `spring-framework-reference.pdf` 为Spring框架的官方参考文档,虽然不是直接关于Thymeleaf,但理解Spring框架的基础是掌握Thymeleaf与Spring集成的关键。 5. **模板使用**: - `thymeleaf模板的使用.pdf` 会...

    JAVA后台管理系统-集成了Thymeleaf、Spring MVC、Shiro、MyBatis、quartz

    JAVA后台管理系统-集成了Thymeleaf、Spring MVC、Shiro、MyBatis、quartz

    SpringBoot快速整合ShiroRedis与Thymeleaf(完整版)免费下载

    在本资源中,我们主要探讨如何将Spring Boot与Shiro安全框架、Redis缓存系统以及Thymeleaf模板引擎进行整合,以构建一个高效的后端应用程序。以下是对这些技术及整合过程的详细说明: 1. **Spring Boot**: Spring...

    springboot+mybatis+gradle+thymeleaf+springsecurity

    在Spring Boot应用中,Thymeleaf可以与Spring MVC无缝集成,用于动态渲染页面,提供丰富的表达式语言和条件逻辑,为前端开发带来便利。 **Spring Security**: Spring Security是Spring生态系统中的一个强大的安全...

    spring_boot使用的 模版引擎 thymeleaf3.0

    Thymeleaf与Spring Framework的集成提供了强大的模板渲染能力,可以让开发者在Spring MVC控制器中定义的映射方法直接转发到Thymeleaf管理的模板。Spring集成的特性允许开发者在模板中使用Spring Expression Language...

    thymeleaf-spring:用于Spring的Thymeleaf集成模块

    Thymeleaf-Spring是Thymeleaf模板引擎与Spring框架深度集成的模块,它使得在Spring应用中使用Thymeleaf变得简单而高效。Thymeleaf是一种强大的服务器端HTML模板引擎,它支持XML、HTML5和其他文本格式。Thymeleaf的...

    Spring mvc spring security Thymeleaf and Bootstrap

    Thymeleaf是一个现代的服务器端HTML模板引擎,常与Spring MVC一起使用。它允许开发者在HTML中直接写入模板表达式,从而将动态数据和静态结构紧密结合。Thymeleaf支持条件语句、循环、国际化等功能,使得模板代码更易...

    基于springboot+thymeleaf+spring data jpa+druid+bootstrap+layui

    在本项目中,Thymeleaf将与Spring Boot集成,提供视图层的处理,使得前端界面和后端逻辑分离,提高代码可读性和可维护性。 **3. Spring Data JPA** Spring Data JPA是Spring框架的一部分,它为Java Persistence API...

    Thymeleaf所需要的jar包

    - `thymeleaf-spring5.jar`(或`thymeleaf-servlet.jar`,取决于Spring版本):这个库是Thymeleaf与Spring框架集成的关键。它允许Thymeleaf与Spring MVC无缝协作,提供模型数据注入、视图解析等功能。 4. **...

Global site tag (gtag.js) - Google Analytics