Thymeleaf 详解
(一)Thymeleaf 是个什么?
<dependency><groupId>org.thymeleaf</groupId><artifactId>thymeleaf</artifactId><version>2.1.4</version> </dependency>
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"xmlns:th="http://www.thymeleaf.org">
3)就可以用th标签动态替换掉静态数据了。如下图,后台传出的message会将静态数据“Red Chair”替换掉,若访问静态页面,则显示数据“Red Chair”。
<td th:text="${message}">Red Chair</td>
2.整合spring
1)加入thymeleaf-spring4-2.1.4.RELEASE.jar(http://www.thymeleaf.org/download.html )包,若用maven,则加入如下配置
<dependency><groupId>org.thymeleaf</groupId><artifactId>thymeleaf-spring3</artifactId><version>2.1.4</version> </dependency>
2)在servlet配置文件中加入如下代码
![](/assets/blank.gif)
<!-- Scans the classpath of this application for @Components to deploy as beans --><context:component-scan base-package="com.test.thymeleaf.controller" /><!-- Configures the @Controller programming model --><mvc:annotation-driven /><!--Resolves view names to protected .jsp resources within the /WEB-INF/views directory --><!--springMVC+jsp的跳转页面配置--><!--<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">--><!--<property name="prefix" value="/WEB-INF/views/" />--><!--<property name="suffix" value=".jsp" />--><!--</bean>--><!--springMVC+thymeleaf的跳转页面配置--><bean id="templateResolver"class="org.thymeleaf.templateresolver.ServletContextTemplateResolver"><property name="prefix" value="/WEB-INF/views/" /><property name="suffix" value=".html" /><property name="templateMode" value="HTML5" /></bean><bean id="templateEngine"class="org.thymeleaf.spring4.SpringTemplateEngine"><property name="templateResolver" ref="templateResolver" /></bean><bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver"><property name="templateEngine" ref="templateEngine" /></bean>
![](/assets/blank.gif)
3)将静态页面加到项目中,更改文件头,加入th标签即可。
3.th标签整理
1)简单表达式
--变量表达式 ${……}
<input type="text" name="userName" value="James Carrot" th:value="${user.name}" />
上述代码为引用user对象的name属性值。
--选择/星号表达式 *{……}
<div th:object="${session.user}"> <p>Nationality: <span th:text="*{nationality}">Saturn</span>.</p> </div>
选择表达式一般跟在th:object后,直接取object中的属性。
--文字国际化表达式 #{……}
<p th:utext="#{home.welcome}">Welcome to our grocery store!</p>
调用国际化的welcome语句,国际化资源文件如下
![](/assets/blank.gif)
resource_en_US.properties: home.welcome=Welcome to here! resource_zh_CN.properties: home.welcome=欢迎您的到来!
![](/assets/blank.gif)
-- URL表达式 @{……}
<a href="details.html" th:href="@{/order/details(orderId=${o.id})}">view</a>
<img src="../../static/assets/images/qr-code.jpg" th:src="@{${path}}" alt="二维码" />
2)常用的th标签
--简单数据转换(数字,日期)
<dt>价格</dt><dd th:text="${#numbers.formatDecimal(product.price, 1, 2)}">180</dd><dt>进货日期</dt><dd th:text="${#dates.format(product.availableFrom, 'yyyy-MM-dd')}">2014-12-01</dd>
--字符串拼接
<dd th:text="${'$'+product.price}">235</dd>
--转义和非转义文本
当后台传出的数据为“This is an <em>HTML</em> text. <b>Enjoy yourself!</b>”时,若页面代码如下则出现两种不同的结果
<div th:text="${html}">
This is an <em>HTML</em> text. <b>Enjoy yourself!</b>
</div> <div th:utext="${html}">
This is an <em>HTML</em> text. <b>Enjoy yourself!</b>
</div>
--表单中
![](/assets/blank.gif)
<form th:action="@{/bb}" th:object="${user}" method="post" th:method="post"><input type="text" th:field="*{name}"/><input type="text" th:field="*{msg}"/><input type="submit"/> </form>
![](/assets/blank.gif)
--显示页面的数据迭代
![](/assets/blank.gif)
//用 th:remove 移除除了第一个外的静态数据,用第一个tr标签进行循环迭代显示<tbody th:remove="all-but-first">//将后台传出的 productList 的集合进行迭代,用product参数接收,通过product访问属性值<tr th:each="product:${productList}"> //用count进行统计,有顺序的显示<td th:text="${productStat.count}">1</td><td th:text="${product.description}">Red Chair</td><td th:text="${'$' + #numbers.formatDecimal(product.price, 1, 2)}">$123</td><td th:text="${#dates.format(product.availableFrom, 'yyyy-MM-dd')}">2014-12-01</td></tr><tr><td>White table</td><td>$200</td><td>15-Jul-2013</td></tr><tr><td>Reb table</td><td>$200</td><td>15-Jul-2013</td></tr><tr><td>Blue table</td><td>$200</td><td>15-Jul-2013</td></tr></tbody>
![](/assets/blank.gif)
--条件判断
<span th:if="${product.price lt 100}" class="offer">Special offer!</span>
不能用"<”,">"等符号,要用"lt"等替代
![](/assets/blank.gif)
<!-- 当gender存在时,选择对应的选项;若gender不存在或为null时,取得customer对象的name--> <td th:switch="${customer.gender?.name()}"><img th:case="'MALE'" src="../../../images/male.png" th:src="@{/images/male.png}" alt="Male" /> <!-- Use "/images/male.png" image --><img th:case="'FEMALE'" src="../../../images/female.png" th:src="@{/images/female.png}" alt="Female" /> <!-- Use "/images/female.png" image --><span th:case="*">Unknown</span> </td>
![](/assets/blank.gif)
![](/assets/blank.gif)
<!--在页面先显示,然后再在显示的数据基础上进行修改--><div class="form-group col-lg-6"> <label>姓名<span> </span></label> <!--除非resume对象的name属性值为null,否则就用name的值作为placeholder值--> <input type="text" class="form-control" th:unless="${resumes.name} eq '' or ${resumes.name} eq null" data-required="true" th:placeholder="${resumes.name}" /> <!--除非resume对象的name属性不为空,否则就定义一个field方便封装对象,并用placeholder提示--> <input type="text" th:field="${resume.name}" class="form-control" th:unless="${resumes.name} ne null" data-required="true" th:placeholder="请填写您的真实姓名" /></div>
![](/assets/blank.gif)
<!-- 增加class="enhanced"当balance大雨10000 --> <td th:class="${customer.balance gt 10000} ? 'enhanced'" th:text="${customer.balance}">350</td>
--根据后台数据选中select的选项
![](/assets/blank.gif)
<div class="form-group col-lg-6"><label >性别<span> Sex:</span></label><select th:field="${resume.gender}" class="form-control" th:switch="${resumes.gender.toString()}"data-required="true"><option value="男" th:case="'男'" th:selected="selected" >男</option><option value="女" th:case="'女'" th:selected="selected" >女</option><option value="">请选择</option></select></div>
![](/assets/blank.gif)
因为gender是定义的Enum(枚举)类型,所以要用toString方法。用th:switch指定传出的变量,用th:case对变量的值进行匹配。!"请选择"放在第一项会出现永远选择的是这个选项。或者用th:if
![](/assets/blank.gif)
<div class='form-group col-lg-4'><select class='form-control' name="skill[4].proficiency"><option >掌握程度</option><option th:if="${skill.level eq '一般'}" th:selected="selected">一般</option><option th:if="${skill.level eq '熟练'}" th:selected="selected">熟练</option><option th:if="${skill.level eq '精通'}" th:selected="selected">精通</option></select> </div>
![](/assets/blank.gif)
--spring表达式语言
![](/assets/blank.gif)
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"><head><title>Thymeleaf tutorial: exercise 10</title><link rel="stylesheet" href="../../../css/main-static.css" th:href="@{/css/main.css}" /><meta charset="utf-8" /></head><body><h1>Thymeleaf tutorial - Solution for exercise 10: Spring Expression language</h1><h2>Arithmetic expressions</h2><p class="label">Four multiplied by minus six multiplied by minus two module seven:</p><p class="answer" th:text="${4 * -6 * -2 % 7}">123</p><h2>Object navigation</h2><p class="label">Description field of paymentMethod field of the third element of customerList bean:</p><p class="answer" th:text="${customerList[2].paymentMethod.description}">Credit card</p><h2>Object instantiation</h2><p class="label">Current time milliseconds:</p><p class="answer" th:text="${new java.util.Date().getTime()}">22-Jun-2013</p><h2>T operator</h2><p class="label">Random number:</p><p class="answer" th:text="${T(java.lang.Math).random()}">123456</p></body> </html>
![](/assets/blank.gif)
--内联
![](/assets/blank.gif)
<label for="body">Message body:</label> <textarea id="body" name="body" th:inline="text"> Dear [[${customerName}]],it is our sincere pleasure to congratulate your in your birthday:Happy birthday [[${customerName}]]!!!See you soon, [[${customerName}]].Regards,The Thymeleaf team </textarea>
![](/assets/blank.gif)
--内联JS <js起止加入如下代码,否则引号嵌套或者"<"">"等不能用>
/*<![CDATA[*/ …… /*]]>*/
--js附加代码:
/*[+ var msg = 'This is a working application'; +]*/
--js移除代码:
/*[- */ var msg = 'This is a non-working template'; /* -]*/
4.不常用
--表达式
2)文字
--表达式基本对象
--给特定的属性设值
th:utext
th:with
th:attr
th:[tagAttr]
可以一次设置两个属性,比如:th:alt-title="#{logo}"
对属性增加前缀和后缀,用th:attrappend,th:attrprepend,比如:th:attrappend="class=${' '+cssStyle}"
对于属性是有些特定值的,比如checked属性,thymeleaf都采用bool值,比如th:checked=${user.isActive}
th:each
th:if or th:unless
th:switch,th:case
选择语句。 th:case="*"表示default case。
原文地址:http://www.cnblogs.com/vinphy/p/4674247.html
Thymeleaf 详解相关推荐
- SpringBoot第十篇:thymeleaf详解
作者:追梦1819 原文:https://www.cnblogs.com/yanfei1819/p/10931435.html 版权声明:本文为博主原创文章,转载请附上博文链接! 引言 Sprin ...
- SpringBoot下的模板技术Thymeleaf详解
Thymeleaf是一个流行的模板引擎,该模板引擎采用Java语言开发. 模板引擎是一个技术名词,是跨领域跨平台的概念,在Java语言体系下有模板引擎,在C#.PHP语言体系下也有模板引擎,在Java ...
- Java基础总结之Thymeleaf详解
一.Thymeleaf语法 1.1 变量表达式${-} 1.2 选择变量表达式*{-} 1.3 消息表达式 #{-} 1.4 链接表达式 @{-} 1.5 片段表达式 ~{-} 二.基本使用 2.1 ...
- Thymeleaf详解
文章目录 一.简介 二.创建Thymeleaf 1. 添加依赖 2. 基本配置 3. 后端通过Model传值 4. 创建Thymeleaf文件 三.Thymeleaf表达式 1. 标准变量表达式 (1 ...
- spring boot(四):thymeleaf使用详解
spring boot(四):thymeleaf使用详解 在上篇文章springboot(二):web综合开发中简单介绍了一下thymeleaf,这篇文章将更加全面详细的介绍thymeleaf的使用. ...
- Thymeleaf语法详解
本文主要介绍下Thymeleaf的基本使用的语法. Thymeleaf语法详解 1.变量输出与字符串操作 1.1 基本用法 表达式 说明 th:text 在页面中输出值 th:value 可以将一 ...
- SpringBoot注解最全详解(整合超详细版本)
今日推荐强制双休!腾讯调整加班机制,21 点前必须离开工位 使用雪花id或uuid作为Mysql主键,被老板怼了一顿! 盘点 12 个 GitHub 上的高仿项目 CTO 说了,用错 @Autowir ...
- spring boot模板引擎thymleaf用法详解
spring boot模板引擎thymleaf用法详解 Spring-boot支持FreeMarker.Thymeleaf.jsp.veocity 但是对freemarker和thymeleaf的支持 ...
- SpringBoot的配置详解application
SpringBoot的配置文件application有两种文件格式,两种配置的内容是一致的,只是格式不一致. 1.application.properties 2.application.yml或者a ...
最新文章
- 面试官问你想找什么工作_找工作时如何面试面试官
- 【ACM】Doubly Linked List(STL list)
- centos 安装java web_centos6.9 安装JAVA-WEB环境
- 文巾解题 372. 超级次方
- oracle恢复某个表的数据库,如何从rman的全备份中恢复一张表?
- 求旋转数组中的最小值
- spring控制事务:声明式事务(注解)
- c# 字节十六进制转十进制_用C中的十进制,八进制和十六进制数字初始化字节数组...
- 开机出现GRUB error22的解决办法
- EPON中ONU版本对冲的故障定位与分析解决
- webpack使用优化(基本篇
- adb shell操作文件
- long 雪花算法_深入分析mysql为什么不推荐使用uuid或者雪花id作为主键
- 数据存储之SwiftJSON
- autocad2007二维图画法_AutoCAD2007教程(二)二维基本绘图命令
- Apache-Commons-FileIOUtils工具类常用方法使用
- 草图大师SketchUp pro 2018中文版
- 我国计算机发展历程简述,简述计算机的发展历程??
- Python绘制双对数曲线
- win7右键计算机死机,win7系统右键死机的解决方法
热门文章
- 辰视冯良炳博士出席演讲的AI+5G+机器视觉 赋能智能制造论坛圆满结束!
- 步进电机驱动数控十字滑台直线、圆弧插补(附斜椭圆插补代码实现)
- 增强现实技术与骁龙空间:从理论到实践
- 保留扇区读写java,FAT文件系统分析(转载)
- 《Java项目实践》:简单聊天程序
- JS定时器和清除定时器/JS四
- 计算机主流处理器介绍,如何选择CPU处理器 选择CPU处理器技巧介绍【详解】
- 利用编译器宏完美的输出调试信息
- Boinx FotoMagico for Mac(电子相册制作工具)破解版安装
- Python/Django 大文件下载问题