Thymeleaf 页面表达式基础
转自:http://www.cnblogs.com/vinphy/p/4674247.html#undefined
(一)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配置文件中加入如下代码

<!-- 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>

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语句,国际化资源文件如下

resource_en_US.properties:
home.welcome=Welcome to here!
resource_zh_CN.properties: home.welcome=欢迎您的到来!

-- 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>
--表单中

<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>

--显示页面的数据迭代

//用 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>

--条件判断
<span th:if="${product.price lt 100}" class="offer">Special offer!</span>
不能用"<”,">"等符号,要用"lt"等替代
<!-- 当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>

<!--在页面先显示,然后再在显示的数据基础上进行修改-->
<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>

<!-- 增加class="enhanced"当balance大雨10000 -->
<td th:class="${customer.balance gt 10000} ? 'enhanced'" th:text="${customer.balance}">350</td>
--根据后台数据选中select的选项

<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>

因为gender是定义的Enum(枚举)类型,所以要用toString方法。用th:switch指定传出的变量,用th:case对变量的值进行匹配。!"请选择"放在第一项会出现永远选择的是这个选项。或者用th:if

<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>

--spring表达式语言

<!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>

--内联

<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>

--内联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
Thymeleaf 页面表达式基础的更多相关文章
- Springboot+JdbcTemplate +thymeleaf 页面 做迷你版的bug系统
https://www.cnblogs.com/qianjinyan/p/10065160.html 在我上一篇随笔中介绍了关于要做的系统的数据结构,连接如上 今天实现连接mssql server, ...
- idea thymeleaf页面变量报错解决
IDEA在thymeleaf页面编写变量,如${user.id}会出现红色波浪下划线错误,提示Validates unresolved references and invalid expressio ...
- 初步认识thymeleaf:简单表达式和标签(一)
初步认识Thymeleaf:简单表达式和标签.(一) 本文只适用于不会Java对HTML语言有基础的程序员们,是浏览了各大博客后收集整理,重新编辑的一篇文章,希望能对大家有所帮助.最后本文如果有哪 ...
- thymeleaf 学习笔记-基础篇(中文教程)
(一)Thymeleaf 是个什么? 简单说, Thymeleaf 是一个跟 Velocity.FreeMarker 类似的模板引擎,它可以完全替代 JSP .相较与其他的模板引擎,它有如下 ...
- thymeleaf模板引擎基础使用(转)
刚好项目上用到这个模板引擎,记录以下基础用法. thymeleaf介绍 简单说, Thymeleaf是一个跟Velocity.FreeMarker类似的模板引擎,它可以完全替代JSP .相比其他的模板 ...
- thymeleaf 基本表达式
Thymeleaf 基本表达式 如需了解thymeleaf以及thymeleaf整合spring,请参考<Thymeleaf模板引擎使用>.<Thymeleaf 集成spring&g ...
- 初步认识thymeleaf:简单表达式和标签(二)
1.th:each:循环,<tr th:each="user,userStat:${users}">,userStat是状态变量,有 index,count,size, ...
- jsp、freemarker、velocity、thymeleaf页面方案分析
1.概述在java领域,表现层技术主要有三种, (1)jsp; (2)freemarker; (3)velocity; (4)thymeleaf; 2.jsp优点: 1.功能强大,可以写java代码 ...
- Thymeleaf 标准表达式语法
变量表达式${ } 在控制器中往页面传递几个变量: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 @Controller public class IndexController ...
随机推荐
- DNS解析综合学习案例(附详细答案)
1.用户需把/dev/myvg/mylv逻辑卷以支持磁盘配额的方式挂载到网页目录下2.在网页目录下创建测试文件index.html,内容为用户名称,通过浏览器访问测试3.创建用户账户,对LVM配置磁盘 ...
- numpy中与金融有关的函数
fv函数 计算未来的价值 def fv(rate, nper, pmt, pv, when='end'): ... 参数: rate:存款/贷款每期的利率 nper:存款/贷款期数 pmt:存款/贷款 ...
- win10操作系统下oracle11g客户端/服务端的下载安装配置卸载总结
win10操作系统下oracle11g客户端/服务端的下载安装配置卸载总结 一:前提 注意:现在有两种安装的方式 1. oracle11g服务端(64位)+oracle客户端(32位)+plsql(3 ...
- QR分解迭代求特征值——原生python实现(不使用numpy)
QR分解: 有很多方法可以进行QR迭代,本文使用的是Schmidt正交化方法 具体证明请参考链接 https://wenku.baidu.com/view/c2e34678168884868762d6 ...
- cmd命令行显示中文乱码
cmd命令行显示中文乱码多数是由于字符编码不匹配导致. 1.查看cmd编码方式 方法一.打开cmd,输入chcp命令回车(显示默认编码:活动代码页:936指GBK) 方法二.打开cmd在标题栏单击鼠标 ...
- 【CF1187E】Tree Painting
题目大意:给定一棵 N 个点的树,初始全是白点.要求你做 N 步操作,每一次选定一个与一个黑点相隔一条边的白点,将它染成黑点,然后获得该白点被染色前所在的白色联通块大小的权值.第一次操作可以任意选点, ...
- Windows系统下如何卸载干净mysql
一.在控制面板中卸载mysql软件 二.卸载过后删除C:\Program Files (x86)\MySQL该目录下剩余了所有文件,把mysql文件夹也删了 三.windows+R运行“regedit ...
- WTL项目各种error的解决方法
error RC1015: cannot open include file 'atlres.h' 解决办法: 向“VC项目属性->资源->附加包含目录” 添加WTL的Include目录( ...
- javascript中的原型和原型链(三)
1. 图解原型链 1.1 “铁三角关系”(重点) function Person() {}; var p = new Person(); 这个图描述了构造函数,实例对象和原型三者之间的关系,是原型链的 ...
- Linux命令-文件管理(三)
Linux more命令 Linux more 命令类似 cat ,不过会以一页一页的形式显示,更方便使用者逐页阅读,而最基本的指令就是按空白键(space)就往下一页显示,按 b 键就会往回(bac ...