thymeleaf渲染

1-thymeleaf条件渲染

  • 概述

使用th:if进行条件渲染

  • 代码实现
@RequestMapping("/test01")
public String test01(Model model){
    model.addAttribute("flag1",true);
    model.addAttribute("flag2",false);
    return "demo01";
}

<a href="/toDemo02" th:if="${flag1}">超链接1</a>
<a href="/toDemo02" th:unless="${flag2}">超链接2</a>
<body>
	<!--表达式的结果为布尔类型时,if 直接以它为结果-->
	<p th:if="true">th:if="true"</p>
	
	<!--当表达式结果为null时,则if判定为false-->
	<p th:if="null">th:if="null"</p>
	
	<!--表达式为数字时,不为0,if判定为true,为0,时,if判定为false-->
	<p th:if="11">th:if="11"</p>
	<p th:if="0">th:if="0"</p>
 
	<!--表达式结果为字符串时,如果为 true,则if判定为true;值为 false,则if判定为false-->
	<!--结果为 off、no 等特殊字符串时,if 判定为false-->
	<p th:if="'true'">th:if="'true'"</p>
	<p th:if="'false'">th:if="'false'"</p>
	<p th:if="'off'">th:if="'off'"</p>
	<p th:if="'no'">th:if="'no'"</p>
	<!--表达式结果字符串不为false,off,no时,if判定为true-->
	<p th:if="'Love China'">th:if="'Love China'"</p>
 
	<!--后台传输:model.addAttribute("userList", User.getUsers());-->
	<!--只要 userList 不等于null,则if判定为true,否则为false-->
	<p th:if="${userList}">th:if="${userList}"</p>
 
	<!--后台传输:model.addAttribute("name", "");-->
	<!--字符串为空时,if判定为 true-->
	<p th:if="${name}eq''">name 等于空</p>
	<p th:if="${name}">th:if="${name}"</p>
</body>

  • th:if :如果条件成立
  • th:unless :如果条件不成立
  • flag1成立,也就是为true,超链接1才显示;flag2不成立,也就是为false,超链接2才显示

条件不成立的标签不在DOM树中。

2-thymeleaf分支渲染

  • 概述

使用th:switch进行分支渲染

  • 语法
<span th:switch="${参数名}">
<span th:case="值1">内容1</span>
<span th:case="值2">内容2</span>
<span th:case="值3">内容3</span>
</span>

  • 代码实现
@RequestMapping("/toDemo03")
public String toDemo03(Model model){
    model.addAttribute("roleName","admin");
    return "demo03";
}


 

<span th:switch="${roleName}">
<span th:case="admin">管理员</span>
<span th:case="kefu">客服</span>
<span th:case="customer">用户</span>
</span>


3-thymeleaf列表渲染

  • 概述

使用th:each进行列表渲染

th:each属性用于迭代循环,语法:th:each="obj,iterStat:${objList}"

迭代对象可以是java.util.List,java.util.Map,数组等;

iterStat称作状态变量,属性有:
    index:当前迭代对象的index(从0开始计算)
    count: 当前迭代对象的index(从1开始计算)
    size:被迭代对象的大小
    current:当前迭代变量
    even/odd:布尔值,当前循环是否是偶数/奇数(从0开始计算)
    first:布尔值,当前循环是否是第一个
    last:布尔值,当前循环是否是最后一个

  • 代码实现
@RequestMapping("/test02")
public String test02(Model model) {
    List<User> userList = new ArrayList<>();
    User user1 = new User(1, "张三","root", 10000.0);
    User user2 = new User(2, "李四","admin", 20000.0);
    userList.add(user1);
    userList.add(user2);
    model.addAttribute("userList", userList);
    return "demo01";
}
<table>
    <tr>
        <td>脚标</td>
        <td>ID</td>
        <td>姓名</td>
        <td>密码</td>
        <td>余额</td>
    </tr>
    <tr th:each="user,userState:${userList}">
        <td th:text="${userState.index}"></td>
        <td th:text="${user.id}">ID</td>
        <td th:text="${user.username}">姓名</td>
        <td th:text="${user.password}">密码</td>
        <td th:text="${user.money}">余额</td>
    </tr>
</table>
    <ol>
		<li>List循环:
			<table border="1">
		      <tr>
		        <th>用户名</th>
		        <th>邮箱</th>
		        <th>管理员</th>
		        <th>状态变量:index</th>
		        <th>状态变量:count</th>
		        <th>状态变量:size</th>
		        <th>状态变量:current.userName</th>
		        <th>状态变量:even</th>
		        <th>状态变量:odd</th>
		        <th>状态变量:first</th>
		        <th>状态变量:last</th>
		      </tr>
		      <tr  th:each="user,userStat : ${list}">
		        <td th:text="${user.userName}">Onions</td>
		        <td th:text="${user.email}">test@test.com.cn</td>
		        <td th:text="${user.isAdmin}">yes</td>
		         <th th:text="${userStat.index}">状态变量:index</th>
		        <th th:text="${userStat.count}">状态变量:count</th>
		        <th th:text="${userStat.size}">状态变量:size</th>
		        <th th:text="${userStat.current.userName}">状态变量:current</th>
		        <th th:text="${userStat.even}">状态变量:even****</th>
		        <th th:text="${userStat.odd}">状态变量:odd</th>
		        <th th:text="${userStat.first}">状态变量:first</th>
		        <th th:text="${userStat.last}">状态变量:last</th>
		      </tr>
	    	</table>
		</li>
		<li>Map循环:
			<div th:each="mapS:${map}">
			<div th:text="${mapS}"></div>
			</div>
		</li>
		<li>数组循环:
			<div th:each="arrayS:${arrays}">
			<div th:text="${arrayS}"></div>
			</div>
		</li>
	</ol>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Thymeleaf 是一种模板引擎,用于在 Web 应用程序中渲染 HTML、XML、JS、CSS 和文本等内容。它是一种服务器端渲染的模板引擎,可以将模板文件作为 HTML 输出到浏览器。 以下是使用 Thymeleaf 渲染 HTML 的示例: 1. 添加 Thymeleaf 依赖 在 Maven 项目中,添加以下依赖: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> ``` 2. 创建 Thymeleaf 模板文件 在 src/main/resources/templates 目录下创建一个名为 index.html 的文件,并添加以下内容: ``` <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Thymeleaf Example</title> </head> <body> <h1 th:text="${message}"></h1> </body> </html> ``` 这个模板文件中包含一个 h1 标签,使用 Thymeleaf 表达式来显示一个变量 message 的值。 3. 编写控制器 创建一个名为 HomeController 的控制器类,添加 @Controller 注解,并创建一个方法来处理请求: ``` @Controller public class HomeController { @RequestMapping("/") public String home(Model model) { model.addAttribute("message", "Hello Thymeleaf!"); return "index"; } } ``` 这个方法将一个名为 message 的变量添加到 Model 中,并返回 index.html 模板文件的名称。 4. 运行应用程序 现在,可以运行应用程序并访问 http://localhost:8080/,将看到一个包含 "Hello Thymeleaf!" 文本的页面。 以上就是使用 Thymeleaf 渲染 HTML 的基本步骤。在实际应用中,可以使用更复杂的 Thymeleaf 表达式和指令来渲染页面,例如循环、条件语句、表单处理等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

穗余

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值