SpringBoot集成Thymeleaf及Thymeleaf常见语法

一、Thymeleaf概述

1.1 Thymeleaf是一个流行的模板引擎,该模板引擎采用Java语言开发;
1.2 模板引擎是一个技术名词,是跨领域跨平台的概念,在Java语言体系下有模板引擎,在C#、PHP语言体系下也有模板引擎,甚至在JavaScript中也会用到模板引擎技术;
1.3 Java生态下的模板引擎有 Thymeleaf 、Freemaker、Velocity、Beetl(国产) 等;
1.4 Thymeleaf模板既能用于web环境下,也能用于非web环境下,在非web环境下,它能直接显示模板上的静态数据,在web环境下,它能像JSP一样从后台接收数据并替换掉模板上的静态数据;
1.5 Thymeleaf 它是基于HTML的,以HTML标签为载体,Thymeleaf 要寄托在HTML的标签下实现对数据的展示;
1.6 Spring boot 集成了thymeleaf模板技术,并且spring boot官方也推荐使用thymeleaf来替代JSP技术;

二、集成步骤

2.1 第一步:在Maven中引入Thymeleaf的依赖,加入以下依赖配置即可:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

2.2 第二步:在Spring boot的核心配置文件application.properties中对Thymeleaf进行配置:

#开发阶段,建议关闭thymeleaf的缓存
spring.thymeleaf.cache=false

#使用遗留的html5以去掉对html标签的校验
spring.thymeleaf.mode=LEGACYHTML5

ps:在使用springboot的过程中,如果使用thymeleaf作为模板文件,则要求HTML格式必须为严格的html5格式,
必须有结束标签,否则会报错;如果不想对标签进行严格的验证,使用spring.thymeleaf.mode=LEGACYHTML5
去掉验证,去掉该验证,则需要引入如下依赖,否则会报错:
<dependency>
    <groupId>net.sourceforge.nekohtml</groupId>
    <artifactId>nekohtml</artifactId>
</dependency>
<dependency>
    <groupId>org.unbescape</groupId>
    <artifactId>unbescape</artifactId>
    <version>1.1.5.RELEASE</version>
</dependency>
ps:NekoHTML是一个Java语言的 HTML扫描器和标签补全器 ,这个解析器能够扫描HTML文件并“修正”HTML文档
中的常见错误。NekoHTML能增补缺失的父元素、自动用结束标签关闭相应的元素,修复不匹配的内嵌元素标签等;

2.3 第三步:写一个Controller去映射到模板页面(和SpringMVC基本一致),比如:

@RequestMapping("/index")
public String index (Model model) {
    model.addAttribute("data", "恭喜,Spring boot集成 Thymeleaf成功!");
    //return 中就是你页面的名字(不带.html后缀)
    return "index";
}

2.4 第四步:在src/main/resources的templates下新建一个index.html页面用于展示数据:HTML页面的元素中加入以下属性:xmlns:th=“http://www.thymeleaf.org

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>Spring boot集成 Thymeleaf</title>
</head>
<body>
<p th:text="${data}">Spring boot集成 Thymeleaf</p>
</body>
</html>

ps:Springboot使用thymeleaf作为视图展示,约定将模板文件放置在src/main/resource/templates目录下,静态资源放置在src/main/resource/static目录下

三、Thymeleaf 常用语法

3.1 变量输出与字符串操作
3.1.1 th:text

th:text
在页面中输出值

3.1.2 th:value

th:value
可以将一个值放入到 input 标签的 value 中

3.1.3 判断字符串

Thymeleaf 内置对象
注意语法:
1,调用内置对象一定要用#
2,大部分的内置对象都以 s 结尾 strings、numbers、dates

${#strings.isEmpty(key)}
判断字符串是否为空,如果为空返回 true,否则返回 false

${#strings.contains(msg,'T')}
判断字符串是否包含指定的子串,如果包含返回 true,否则返回 false

${#strings.startsWith(msg,'a')}
判断当前字符串是否以子串开头,如果是返回 true,否则返回 false

${#strings.endsWith(msg,'a')}
判断当前字符串是否以子串结尾,如果是返回 true,否则返回 false

${#strings.length(msg)}
返回字符串的长度

${#strings.indexOf(msg,'h')}
查找子串的位置,并返回该子串的下标,如果没找到则返回-1

${#strings.substring(msg,13)}
${#strings.substring(msg,13,15)}
截取子串,用户与 jdk String 类下 SubString 方法相同

${#strings.toUpperCase(msg)}
${#strings.toLowerCase(msg)}
字符串转大小写。

3.2 日期格式化处理

${#dates.format(key)}
格式化日期,默认的以浏览器默认语言为格式化标准

${#dates.format(key,'yyy/MM/dd')}
按照自定义的格式做日期转换

${#dates.year(key)}
${#dates.month(key)}
${#dates.day(key)}
year:取年
Month:取月
Day:取日

3.3 条件判断
3.3.1 th:if

<span th:if="${sex} == '男'">
性别:男
</span>
<span th:if="${sex} == '女'">
性别:女
</span>

3.3.2 th:switch

<div th:switch="${id}">
	<span th:case="1">ID 为 1</span>
	<span th:case="2">ID 为 2</span>
	<span th:case="3">ID 为 3</span>
</div>

3.4 迭代遍历
3.4.1 th:each

@RequestMapping("/show3")
public String showInfo3(Model model){
List<Users> list = new ArrayList<>();
	list.add(new Users(1,"张三",20));
	list.add(new Users(2,"李四",22));
	list.add(new Users(3,"王五",24));
	model.addAttribute("list", list);
	return "index3";
}

<table border="1">
<tr>
	<th>ID</th>
	<th>Name</th>
	<th>Age</th>
</tr>
<tr th:each="u : ${list}">
	<td th:text="${u.userid}"></td>
	<td th:text="${u.username}"></td>
	<td th:text="${u.userage}"></td>
</tr>
</table>

3.4.2 ht:each 状态变量

@RequestMapping("/show3")
public String showInfo3(Model model){
	List<Users> list = new ArrayList<>();
	list.add(new Users(1,"张三",20));
	list.add(new Users(2,"李四",22));
	list.add(new Users(3,"王五",24));
	model.addAttribute("list", list);
	return "index3";
}
<table border="1">
<tr>
	<th>ID</th>
	<th>Name</th>
	<th>Age</th>
	<th>Index</th>
	<th>Count</th>
	<th>Size</th>
	<th>Even</th>
	<th>Odd</th>
	<th>First</th>
	<th>lase</th>
</tr>
<tr th:each="u,var : ${list}">
	<td th:text="${u.userid}"></td>
	<td th:text="${u.username}"></td>
	<td th:text="${u.userage}"></td>
	<td th:text="${var.index}"></td>
	<td th:text="${var.count}"></td>
	<td th:text="${var.size}"></td>
	<td th:text="${var.even}"></td>
	<td th:text="${var.odd}"></td>
	<td th:text="${var.first}"></td>
	<td th:text="${var.last}"></td>
</tr>
</table>

状态变量属性
1,index:当前迭代器的索引 从 0 开始
2,count:当前迭代对象的计数 从 1 开始
3,size:被迭代对象的长度
4,even/odd:布尔值,当前循环是否是偶数/奇数 从 0 开始
5,first:布尔值,当前循环的是否是第一条,如果是返回 true 否则返回 false
6,last:布尔值,当前循环的是否是最后一条,如果是则返回 true 否则返回 false

3.4.3 th:each 迭代 Map

@RequestMapping("/show4")
public String showInfo4(Model model){
	Map<String, Users> map = new HashMap<>();
	map.put("u1", new Users(1,"张三",20));
	map.put("u2", new Users(2,"李四",22));
	map.put("u3", new Users(3,"王五",24));
	model.addAttribute("map", map);
	return "index4";
}

<table border="1">
	<tr>
		<th>ID</th>
		<th>Name</th>
		<th>Age</th>
	</tr>
	<tr th:each="maps : ${map}">
		<td th:text="${maps}"></td>
	</tr>
</table>
<th/>
<table border="1">
	<tr>
		<th>ID</th>
		<th>Name</th>
		<th>Age</th>
	</tr>
		<tr th:each="maps : ${map}">
			<td th:each="entry:${maps}" th:text="${entry.value.userid}" ></td>
			<td th:each="entry:${maps}" th:text="${entry.value.username}"></td>
			<td th:each="entry:${maps}" th:text="${entry.value.userage}"></td>
	</tr>
</table>

3.5 域对象操作
3.5.1 HttpServletRequest

request.setAttribute("req", "HttpServletRequest");
Request:<span th:text="${#httpServletRequest.getAttribute('req')}"></span><br/>

3.5.2HttpSession

request.getSession().setAttribute("sess", "HttpSession");
Session:<span th:text="${session.sess}"></span><br/>

3.5.3ServletContext

request.getSession().getServletContext().setAttribute("app","Application");
Application:<span th:text="${application.app}"></span>
如果你想在A页面中使用List中的数据作为参数,并通过<a>标签将数据传递到下一个控制器,你可以按照以下步骤进行操作: 首先,在A页面中,你可以使用Thymeleaf模板引擎来遍历List,并在<a>标签中传递数据作为参数。例如: ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>Your A Page</title> </head> <body> <h1>Your A Page</h1> <ul> <li th:each="form : ${formList}"> <a th:href="@{/your-next-controller-url(data=${form.id})}">Click here</a> </li> </ul> </body> </html> ``` 在上面的代码中,`th:each`指令用于遍历List中的数据,`form` 是迭代变量,`${formList}` 是传递给A页面的List。 在`<a>`标签中,`th:href`属性使用Thymeleaf的URL表达式来设置链接的URL。`@{/your-next-controller-url(data=${form.id})}` 表示链接的URL为 `/your-next-controller-url`,并将`form.id`作为参数名为`data`的参数传递给下一个控制器。 然后,在下一个控制器中,你可以使用`@RequestParam`注解来接收传递的参数。例如: ```java @Controller public class YourNextController { @GetMapping("/your-next-controller-url") public String yourNextControllerMethod(@RequestParam("data") int data) { // 在这里处理接收到的数据 // 返回相应的结果或视图 return "your-next-view"; } } ``` 在上面的代码中,`@GetMapping("/your-next-controller-url")` 注解指定了控制器方法处理的URL。`@RequestParam("data")` 注解指定了接收的参数名称。 当用户点击链接时,将会调用`yourNextControllerMethod`方法,并将传递的参数值作为参数传递给该方法。你可以在方法内部处理数据,并返回相应的结果或视图。 请注意,上述示例中的URL和参数名称仅供参考,你需要根据自己的实际情况进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值