Thymeleaf实践操作

项目架构图

在这里插入图片描述

1、加入依赖

<!--Thymeleaf依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
            <version>2.7.3</version>
        </dependency>
        <!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.22</version>
        </dependency>

2、实体类

@Data
@NoArgsConstructor
@AllArgsConstructor

public class User {

    private String name;
    private int age;
    private String honor; 
}

3、控制类

@Controller
public class UserController {
    @RequestMapping("/h1")
    public String test(Model model) {
        
        User user1 = new User("阿狗", 4, "看家");
        User user2 = new User("阿猫", 2, "捉鼠");
        User user3 = new User("阿猪", 1, "贡献自我");
        model.addAttribute("user1", user1);
        model.addAttribute("user2", user2);
        model.addAttribute("user3", user3);
        model.addAttribute("user","Thymeleaf实践操作");
        return "hello";
    }
}

3、页面显示

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>酷小亚</title>
    <style>
        table {
            margin: 30px auto;
            text-align: center;
        }
        th {
            color: red;
            width: 150px;
            height: 40px;
        }
        img {
            width: 120px;
            height: 150px;
        }
    </style>
</head>
<body>
<h2 th:text="${user}" style="text-align: center"></h2>
<table border="1" cellspacing="0">
    <tr>
        <th>照片</th>
        <th>姓名</th>
        <th>年龄</th>
        <th>本领</th>
    </tr>
    <tr>
        <td><img src="../images/dog.webp"></td>
        <td th:text="${user1.name}"></td>
        <td th:text="${user1.age}"></td>
        <td th:text="${user1.honor}"></td>
    </tr>
    <tr>
        <td><img src="../images/cat.webp"></td>
        <td th:text="${user2.name}"></td>
        <td th:text="${user2.age}"></td>
        <td th:text="${user2.honor}"></td>
    </tr>
    <tr>
        <td><img src="../images/pig.webp"></td>
        <td th:text="${user3.name}"></td>
        <td th:text="${user3.age}"></td>
        <td th:text="${user3.honor}"></td>
    </tr>
</table>
</body>
</html>

5、展示效果

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

已转行@

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

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

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

打赏作者

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

抵扣说明:

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

余额充值