thymeleaf 模板引擎 的循环读取

1.pom文件中加入 thymeleaf  的Jar

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

在templates下创建一个html文件

thymeleaf 模板引擎 的循环读取

3.


html代码如下

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>学生集合的循环遍历</title>
</head>
<body>
<h2>用户的列表</h2>
<div>
    <ul>
        <li  th:each="user:${users}">
            <span th:text="${user.id}"></span><br/>
            <span th:text="${user.name}"></span><br/>
            <span th:text="${user.age}"></span><br/>
            <span th:text="${user.getEmail()}"></span><br/>
        </li>
    </ul>
</div>

</body>
</html>

 

注意:xmlns:th="http://www.thymeleaf.org"  这是引入 thymeleaf 的标签库


后台传过来的值如下:

thymeleaf 模板引擎 的循环读取

前台循环读取后台的值:

<li th:each="user:${users}">

<span th:text="${user.id}"></span><br/>

<span th:text="${user.name}"></span><br/>

<span th:text="${user.age}"></span><br/>

<span th:text="${user.getEmail()}"></span><br/>

</li>


${users}代表后台传过来的值,

user是${users} 他的别名

获得他的文本user.id


Git地址:

https://gitee.com/Super_Ren/thymeleaf-MyBatis-RestFul