内联

hello , [[${userName}]]

等价于

内联

<" />

小言_互联网的博客

thymeleaf进阶学习

264人阅读  评论(0)

使用内联文本表示时,必须先用在th:inline="text/javascript/none"激活

<div>
	<h1>内联</h1>
	<div th:inline="text">
		<p>hello , [[${userName}]]</p>
	</div>
</div>

等价于

<div>
	<h1>内联</h1>
	<p th:text="'hello'+${userName}"></p>
</div>

脚本内联

<script th:inline="javascript">
	var name = [[${userName}]] + ',welcome';
	alert(name);
</script>

基本对象
#ctx 上下文对象
#vars 上下文变量
#locale 区域对象
#request (仅web环境可用) HttpServletRequest对象
#response (仅web环境可用) HttpServletResponse对象
#session (仅web环境可用) HttpSession对象
#servletContext (仅web环境可用) ServletContext对象

<p th:text="${#request.getAttribute('name')}"></p>
<p th:text="${session.name}"></p>
<span th:text="${#locale.country}"></span>

session可以直接.属性

内嵌变量,通过#直接访问
dates java.util.Date的功能方法类
calendars 面向java.util.Calendar
numbers 格式化数字的功能方法类
strings 字符串对象的功能类 contains,startWiths,prepending,appending
objects 对objects的功能类操作
bools 对布尔值求值的功能方法
arrays 对数组的功能方法
lists 对lists的功能方法
sets 对set的实用方法
maps 对map的实用方法

dates
格式化时间

<p th:text="${#dates.format(date,'yyyy-MM-dd HH:mm:ss')}"></p>

创建当前时间,精确到天

<p th:text="${#dates.createToday()}"></p>

创建当前时间,精确到秒

<p th:text="${#dates.createNow()}"></p>

strings
判断是否为空

<p th:text="$(#strings.isEmpty(userName))"></p>

判断list是否为空

<p th:text="${#strings.listIsEmpty(users)}"></p>

输出字符串长度

<p th:text="${#strings.length(userName)}"></p>

拼接字符串

<p th:text="${#strings.concat(userName,password)}"></p>

创建自定长度的字符串

<p th:text="${#strings.randomAlphanumeric(count)}"></p>

表达式

变量表达式 ${}
选择或星号表达式 *{}
文字国际化表达式 #{}
URL表达式 @{}
片段表达式 ~{}

关于片段表达式,最常用的是th:insert和th:replace (不过开发的时候用的较少)

<div th:insert="~{commons :: main}"></div>

也可以在页面的其他位置去使用

<div th:with="frag=~{footer :: #main/text()}">
	<p th:insert="${frag}"></p>
</div>

具体结合Spring boot的时候,还需要了解thymeleaf的默认配置,要学好thymeleaf模板引擎,建议多练习,开发上经常使用内嵌变量,这个是熟系的重点和难点。毕竟thymeleaf是非常强大的,平时开发的时候只需要掌握80%就够用了,高级的内容涉及到自定义函数指令,自定义模板等。


转载:https://blog.csdn.net/huangbaokang/article/details/101034685
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场