JSP进阶---->:Thymeleaf:快速掌握

  1. 创建使用

<!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> studentsnull,页面将无法显示表单,后台必须给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}"

 

 

 

 

 

 

 

posted on   白嫖老郭  阅读(499)  评论(0编辑  收藏  举报

编辑推荐:
· 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代理技术深度解析与实战指南

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示