thymeleaf语法总结

文件参考:https://www.jianshu.com/p/f79a98173677

1.基本表达式

1.1:变量表达式${}

<span th:text="${information}"></span>

1.2:选择变量表达式*{}:一般配合@ModelAttribute使用,注解用法

//java的controller
@RequestMapping(value = "/login", method = RequestMethod.POST)
public String login(@ModelAttribute(value = "loginBean") LoginBean loginBean,ModelMap model) {...}

//thymeleaf的html页面
<form id="login-form" th:action="@{/login}" th:object="${loginBean}">...
<input type="text" value="" th:field="*{username}"></input>
<input type="text" value="" th:field="*{user[0].username}"></input>
</form>

1.3:信息表达式#{}

<p th: text=" #{home. welcome}" >This text will not be show! </p>

home.welcome=this messages is from home.properties!

1.4:链接URL表达式@{}

<link th:src="@{/resources/css/bootstrap.min.css}" />

<li><a th:href="@{/forIndex}">首页</a></li>

1.5:工具类表达式 ${#map} (详情参考

<div th:if="${#maps.size(stu)!=0}">
...do something...
</div>

2.常用属性

th:action      表单提交的地址              <form action="subscribe.html" th:action="@{/subscribe}">
th:each        属性赋值                <tr th:each="user,userStat:${users}">
th:field       常用于表单字段绑定             <input type="text" value="" th:field="*{username}"></input>
th:href        链接地址                <a th:href="@{/login}" th:unless=${session.user != null}>Login</a> />
th:id         替换id                    <input th:id="'xxx' + ${collect.id}"/>
th:if          判断条件                   <a th:if="${userId == collect.userId}" >
th:include     布局标签,替换内容到引入的文件     <head th:include="layout :: htmlhead" th:with="title='xx'"></head> />
th:fragment    布局标签,定义代码片段,其它引用   <div th:fragment="alert">
th:object      替换对象                     <div th:object="${session.user}">
th:src         图片类地址引入                 ![](@{/img/logo.png})
th:replace     布局标签,替换整个标签到引入的文件  <div th:replace="fragments/header :: title"></div>
th:text       文本替换                      <p th:text="${collect.description}">description</p>
th:value       属性赋值                      <input th:value="${user.name}" />
th:inline     js脚本中可以使用thymeleaf变量           <script type="text/javascript" th:inline="javascript">
th:remove      删除某个属性                 <tr th:remove="all"> 
th:style       设置样式                  th:style="'display:' + @{(${sitrue} ? 'none' : 'inline-block')} + ''"
th:onclick     点击事件                   th:onclick="'getCollect()'"

3.标准表达式

3.1:普通字面

<span th:text="'working web application'">template file</span>	//working web application
<div th:if="${user.isAdmin()} == false">						//true
<div th:if="${variable.something} == null">  					//null

3.2:文字操作

<span th:text="'Welcome to our application, ' + ${user.name} + '!'"> //Welcome to our application,liming
<span th:text="|Welcome to our application, ${user.name}!|">//Welcome to our application,liming

3.3:算数运算
3.3.1:二元运算 +,-,*,/,%

<span th:text="1+1">1+1</span>

3.3.2:布尔运算 and ,or, !, not

<span  th:if="${!#lists.isEmpty(list)} and ${#lists.isEmpty(list)}" >and</span>
<span  th:if="${!#lists.isEmpty(list)} or ${#lists.isEmpty(list)}" >or</span>
<span  th:if="${!#lists.isEmpty(list)}">not</span>

3.3.3:比较运算 (>,<,<=,>=,==,!=(gt,lt,le,ge,eq,ne))

<ol>   
<li>>(gt)<span th:text="1+1" th:if="${#lists.size(list)} > 1">大于></span>else</li>   
<li>小于lt:<span th:if="${#lists.size(list)} lt 1">小于</span>else</li>   
<li>>=(ge)<span  th:if="${#lists.size(list)} >= 1">大于等于>=</span>else</li>   
<li>小于等于(le)<span  th:if="${#lists.size(list)} le 1">小于等于</span>else</li>   
<li>!(not)<span  th:if="${!#lists.isEmpty(list)}">!(not)</span>else</li>   
<li>==(eq)<span th:text="'Execution mode is ' + ( (${execMode} == 'dev')? 'Development' : 'Production')">等于==</span></li>   
<li>!=(ne/neq):size:<span th:text="${#lists.size(list)}" th:if="${#lists.size(list)} != 1"></span></li>
</ol>

3.3.4:条件运算 if ?then , if? then:else , if ? :default

<span th:class="${title1} ? 'green'">样例</span>
<span th:class="${title} ? 'green' :' red'">样例一</span>
 <span th:text="*{age}?: '(no age specified)'">20</span>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值