JSP进阶---->:Thymeleaf:快速掌握
-
创建使用
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> </html>
获取变量值${…}
th:text
最简单的一种,替换标签中的文本内容
<span th:text="Hello"></span> <span th:text="${msg}">无法显示:</span>
th:value
与th:text类似,它是用来替换表单中的值。
<p> <input type="text" th:value="${msg}"/> </p>
Thymeleaf URL表达式 :
th:href(th:src)
表达式以@开头:@{ 访问路径 }
链接表达式: @{…}
注:一般是配合th:href和th:src
th:src一般用来导入图片
th:href一般用来设置链接或者导入静态文件css等
以上也不是严格固定
<div>
<a th:href="@{/user/showInfo}">link</a>
</div>
条件判断
1. 条件判断 th:unless、th:if
<p> <span th:if="${sex==1}"> 男 </span> <span th:if="${sex==0}"> 女 </span> </p>
2.复杂点条件判断:th:switch、th:case
<p> <div th:switch="${sex}"> <span th:case="1">男</span> <span th:case="0">女</span> </div> </p>
th:each
迭代遍历,a代表每个集合中便利到的对象,s代表对象的状态,:之后是要遍历的集合。
<div> <div th:each="a,s : ${list}"> <span th:text="${a}"></span>, <span th:text="${#strings.concat('index--',s.index)}"></span>, <span th:text="${#strings.concat('count--',s.count)}"></span>, <span th:text="${#strings.concat('first--',s.first)}"></span>, <span th:text="${#strings.concat('last--',s.last)}"></span>, <span th:text="${#strings.concat('current--',s.current)}"></span>, <span th:text="${#strings.concat('odd--',s.odd)}"></span>, <span th:text="${#strings.concat('size--',s.size)}"></span>, </div> </div>
二、Thymeleaf内置对象
Thymeleaf内置对象的语法${# }内部必须以 # 开头
strings
判断字符串是否为空,strings.isEmpty()
<p> <span th:text="${#strings.isEmpty(msg)}"></span> </p>
判断是否包含某个字符串,strings.contains(msg,‘Th’)
<p> <span th:text="${#strings.contains(msg,'Th')}"></span> </p>
判断是否字符串是否以指定字符串开头/结尾的 区分大小写
<p> <span th:text="${#strings.startsWith(msg,'Hello')}"></span> <span th:text="${#strings.endsWith(msg,'Hello')}"></span> </p>
截取字符串,substring()
<p> <span th:text="${#strings.substring(msg,1)}"></span> </p> <p> <span th:text="${#strings.substring(msg,6,9)}"></span> </p>
dates
1.格式化日期
<p> <span th:text="${#dates.format(date,'yyyy-MM-dd HH:mm:ss')}"></span> </p>
2.获取年月日
<p> <span th:text="${#dates.year(date)}"></span> </p> <p> <span th:text="${#dates.month(date)}"></span> </p> <p> <span th:text="${#dates.day(date)}"></span> </p>
Web作用域对象:
1.request <span th:text="${#httpServletRequest.getAttribute('request')}"></span><br/> 2.session <span th:text="${#session.getAttribute('session')}"></span><br/>
3.application <span th:text="${#servletContext.getAttribute('application')}"></span>
Thymeleaf 在 javascript 中的使用
我们可以在js代码内部直接获取返回的值
<script th:inline="javascript"> var request = [[${#servletContext.getAttribute('application')}]]; alert(request); </script>
<form>标签的action属性。:
<form id="login-form" th:action="@{/login}">...</form>
功能类似jstl中的<c:forEach>标签。:
例子中通过选择表达式*{}既能将表单绑定到后台的StudentRequestBean中的集合属性students,也能将Servlet上下文中的StudentRequestBean中的List类型的students变量回显,回显时通过th:each进行遍历。
注意1:绑定集合属性元素下标的用法*{students[__${rowStat.index}__].firstName}
注意2:如果List<Student> students为null,页面将无法显示表单,后台必须给students初始化一个值,即:注意3:stuIter代表students的迭代器
<form id="login-form" th:action="@{/addStudent}" th:object="${stuReqBean}" method="POST"> <div class="student" th:each="stuIter,rowStat:${stuReqBean.students}"> <input type="text" class="firstName" value="" th:field="*{students[__${rowStat.index}__].firstName}"></input> <input type="text" class="school" value="" th:field="*{students[__${rowStat.index}__].school}"></input> ... </div> </form>
public class StudentRequestBean { private List<Student> students; ... } public class Student implements Serializable{ private String firstName; private String school; ...} @RequestMapping(value = "/addStudent", method = RequestMethod.POST) public String addStudent(@ModelAttribute(value = "stuReqBean") StudentRequestBean stuReqBean,ModelMap model) {...}
th:field
常用于表单字段绑定。通常与th:object一起使用。 属性绑定、集合绑定。
复制代码 <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>
th:object
用于表单数据对象绑定,将表单绑定到后台controller的一个JavaBean参数。常与th:field一起使用进行表单数据绑定。
<form id="login-form" th:action="@{/login}" th:object="${loginBean}">...</form>
@RequestMapping(value = "/login", method = RequestMethod.POST)
public String login(@ModelAttribute(value = "loginBean") LoginBean loginBean,ModelMap model) {...}
th:src 用于外部资源引入,类似于<script>标签的src属性,常与@{}一起使用。
<script th:src="@{/resources/js/jquery/jquery.json-2.4.min.js}"
个性签名:独学而无友,则孤陋而寡闻。做一个灵魂有趣的人!
如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!
Java入门到入坟
万水千山总是情,打赏一分行不行,所以如果你心情还比较高兴,也是可以扫码打赏博主,哈哈哈(っ•̀ω•́)っ✎⁾⁾!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南