thymeleaf常用标签
镇楼图
镇楼表格
关键字 | 功能介绍 | 案例 |
---|---|---|
th:id | 替换id | <input th:id="'xxx' + ${collect.id}"/> |
th:text | 文本替换 | <p th:text="${collect.description}">description</p> |
th:utext | 支持html的文本替换 | <p th:utext="${htmlcontent}">conten</p> |
th:object | 替换对象 | <div th:object="${session.user}"> |
th:value | 属性赋值 | <input th:value="${user.name}" /> |
th:with | 变量赋值运算 | <div th:with="isEven=${prodStat.count}%2==0"></div> |
th:style | 设置样式 | th:style="'display:' + @{(${sitrue} ? 'none' : 'inline-block')} + ''" |
th:onclick | 点击事件 | th:onclick="'getCollect()'" |
th:each | 属性赋值 | tr th:each="user,userStat:${users}"> |
th:if | 判断条件 | <a th:if="${userId == collect.userId}" > |
th:unless | 和th:if判断相反 | <a th:href="@{/login}" th:unless=${session.user != null}>Login</a> |
th:href | 链接地址 | <a th:href="@{/login}" th:unless=${session.user != null}>Login</a> /> |
th:switch | 多路选择 配合th:case 使用 | <div th:switch="${user.role}"> |
th:case | th:switch的一个分支 | <p th:case="'admin'">User is an administrator</p> |
th:fragment | 布局标签,定义一个代码片段,方便其它地方引用 | <div th:fragment="alert"> |
th:include | 布局标签,替换内容到引入的文件 | <head th:include="layout :: htmlhead" th:with="title='xx'"></head> /> |
th:replace | 布局标签,替换整个标签到引入的文件 | <div th:replace="fragments/header :: title"></div> |
th:selected | selected选择框 选中 | th:selected="(${xxx.id} == ${configObj.dd})" |
th:src | 图片类地址引入 | <img class="img-responsive" alt="App Logo" th:src="@{/img/logo.png}" /> |
th:inline | 定义js脚本可以使用变量 | <s c r i p t type="text/javas c r i p t" th:inline="javas c r i p t"> |
th:action | 表单提交的地址 | <form action="subscribe.html" th:action="@{/subscribe}"> |
th:remove | 删除某个属性 | <tr th:remove="all"> 1.all:删除包含标签和所有的孩子。2.body:不包含标记删除,但删除其所有的孩子。3.tag:包含标记的删除,但不删除它的孩子。4.all-but-first:删除所有包含标签的孩子,除了第一个。5.none:什么也不做。这个值是有用的动态评估。 |
th:attr | 设置标签属性,多个属性可以用逗号分隔 | 比如th:attr="src=@{/image/aa.jpg},title=#{logo}" ,此标签不太优雅,一般用的比较少。 |
1.URL
a标签url拼接 th:href
th:href="@{/home/{pagePath}/{page}(pagePath=${pagePath},page=1)}"
根据pagePath、page自动生成url比如/home/user/1
th:href="@{/home/pagePath/page(pagePath=${pagePath},page=1)}"
会在末尾加上参数,比如/home/pagePath/page?pagePath=user&page=1
a标签js脚本 th:onclick
href="javascript:void(0)" th:onclick= "'javascript:add('+${info.id}+')'"
多条件,有字符串,使用 \'字符串\'
th:onclick= "'javascript:addLabel(\'label\','+${info.id}+')'"
2.递归
循环递归 th:each
<div th:each="info:${list}"> <div th:text="${info.id}"></div> </div>
th:each自带属性
<div th:each="label,stat:${info.labelInfoList}"> <input th:name="'labelInfoList['+${stat.index}+'].id'" th:value="${label.id}"/> </div>
stat自带属性如下
index:当前迭代对象的 index(从0开始计算)
count: 当前迭代对象的 index(从1开始计算)
size:被迭代对象的大小
current:当前迭代变量
even/odd:布尔值,当前循环是否是偶数/奇数(从0开始计算)
first:布尔值,当前循环是否是第一个
last:布尔值,当前循环是否是最后一个
循环自定义number
<span th:each="i : ${#numbers.sequence(cpage-3,cpage+3)}"> <a th:href="@{{page}(page=${i})}" th:text="${i}" /> </span>
3.判断
判断1 th:switch
<span th:switch="${info.statusCd}"> <span class="art-title-span" th:case="0">无效</span> <span class="art-title-span" th:case="1">需求</span> <span class="art-title-span" th:case="2">设计</span> <span class="art-title-span" th:case="3">开发</span> <span class="art-title-span" th:case="4">测试</span> <span class="art-title-span" th:case="5">上线</span> </span>
th:each 加 th:switch组合使用
<span class="art-title-span" th:switch="${info.statusCd}"> <span th:each="code:${codeList}" class="art-title-span" th:case="${code.codeValue}" th:text="${code.codeName}"></span> </span>
判断2
<span class="page_span" th:style="${cpage-3<=1}? 'display:none':''" >
判断3
<a th:style="${#strings.containsIgnoreCase(#httpServletRequest.getRequestURL(),'/file/list')}? 'background:#2c9678;color:white;':''" th:text="文件管理" th:href="@{/file/list/null}"> </a>
判断4
<span th:if="${info.id.equals('aaa')}" text="显示"/>
4.引入page
引入其他html
<!-- 引入导航栏 --> <div th:include="common/header::pagination"></div> <!-- 引入第二菜单 --> <div th:style="${qid!=null && qid==session.user_session.id}?'':'display:none'" th:include="common/page::my-menu"></div> <!-- 引入标签菜单 --> <div th:style="${qid!=null && qid!=''}?'display:none':''" th:include="common/page::label"></div>
common/header、common/page指向文件,意为common/heade.html、common/page.html,::为th:fragment的名称
<div th:fragment="common-pagination"> <div class="page_div"> 。。。 </div> </div>
5.赋值
th:text,显示字符串内容
<title th:text="${quser!=null}?${quser.realname}+'的文章列表-梦神十夜的小站':'梦神十夜的小站'"></title>
th:utext,显示html内容
<span th:utext="${info.context}"></span>
th:value,显示input等元素的值
<input type="text" name="title" id="title_ue" maxlength="100" placeholder="最多可输入100个字" th:value="${info.title}"/>
获取session的内容
<title th:text="${session.user_session.name}+'的文章列表-梦神十夜的小站':'梦神十夜的小站'"></title>
获取cookie的内容
<span th:each="cookie :${#httpServletRequest.getCookies()}" th:if="${cookie.getName().equals('nightThemeName')}" text="11111"/>
th:object,星号表达式
<div th:object="${session.user}"> <p>Name: <span th:text="*{firstName}">Sebastian</span>.</p> <p>Surname: <span th:text="*{lastName}">Pepper</span>.</p> <p>Nationality: <span th:text="*{nationality}">Saturn</span>.</p> </div>
等价于
<div th:object="${session.user}"> <p>Name: <span th:text="${session.user.firstName}">Sebastian</span>.</p> <p>Surname: <span th:text="${session.user.lastName}">Pepper</span>.</p> <p>Nationality: <span th:text="${session.user.nationality}">Saturn</span>.</p> </div>
美元符号和星号语法可以混合使用:
<div th:object="${session.user}"> <p>Name: <span th:text="*{firstName}">Sebastian</span>.</p> <p>Surname: <span th:text="${session.user.lastName}">Pepper</span>.</p> <p>Nationality: <span th:text="*{nationality}">Saturn</span>.</p> </div>
6.内联JS
在JS中使用变量,需要th:inline="javascript"激活,使用[[…]] 内联文本的表示方式
<s c r ip t th:inline="j a v a s c r i p t"> /*<![CDATA[*/ ... var username = [[${sesion.user.name}]]; var size = [[${size}]]; ... /*]]>*/ </s c r i p t>
7.内嵌变量
使用#变量
dates : java.util.Date的功能方法类。
calendars : 类似#dates,面向java.util.Calendar
numbers : 格式化数字的功能方法类
strings : 字符串对象的功能类,contains,startWiths,prepending/appending等等。
objects: 对objects的功能类操作。
bools: 对布尔值求值的功能方法。
arrays:对数组的功能类方法。
lists: 对lists功能类方法
sets
maps
比如data
/* * Format date with the specified pattern * Also works with arrays, lists or sets */ ${#dates.format(date, 'dd/MMM/yyyy HH:mm')} ${#dates.arrayFormat(datesArray, 'dd/MMM/yyyy HH:mm')} ${#dates.listFormat(datesList, 'dd/MMM/yyyy HH:mm')} ${#dates.setFormat(datesSet, 'dd/MMM/yyyy HH:mm')} /* * Create a date (java.util.Date) object for the current date and time */ ${#dates.createNow()} /* * Create a date (java.util.Date) object for the current date (time set to 00:00) */ ${#dates.createToday()}
String
/* * Check whether a String is empty (or null). Performs a trim() operation before check * Also works with arrays, lists or sets */ ${#strings.isEmpty(name)} ${#strings.arrayIsEmpty(nameArr)} ${#strings.listIsEmpty(nameList)} ${#strings.setIsEmpty(nameSet)} /* * Check whether a String starts or ends with a fragment * Also works with arrays, lists or sets */ ${#strings.startsWith(name,'Don')} // also array*, list* and set* ${#strings.endsWith(name,endingFragment)} // also array*, list* and set* /* * Compute length * Also works with arrays, lists or sets */ ${#strings.length(str)} /* * Null-safe comparison and concatenation */ ${#strings.equals(str)} ${#strings.equalsIgnoreCase(str)} ${#strings.concat(str)} ${#strings.concatReplaceNulls(str)} /* * Random */ ${#strings.randomAlphanumeric(count)}