SpringBoot整合Thymeleaf

SpringBoot整合Thymeleaf

1.在pom中增加依赖

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

2.在resources目录下添加模板文件

<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta http-equiv="content-type" content="text/html;charset=utf-8">
    <title>Thymeleaf</title>
</head>
<body>
<table>
    <tr>
        <td>索引</td>
        <td>目录名称</td>
        <td>状态</td>
    </tr>
    <tr th:each="category:${categories}">
        <td th:text="${category.id}"></td>
        <td th:text="${category.categoryName}"></td>
        <td th:text="${category.status}"></td>
    </tr>
</table>
</body>
</html>

3.现有返回接口改造成Thymeleaf返回接口对比

//    @RequestMapping("/category")
//    @ResponseBody
//    public String getAllCategory() {
//        return categoryService.getAllCategory().toString();
//    }

    @RequestMapping("/category1")
    public String getAllCategory1(Model model) {
        model.addAttribute("categories", categoryService.getAllCategory());
        return "category1";
    }

4.访问category1页面

image-20240611111443647

参考

posted @ 2024-06-11 11:16  litayun  阅读(3)  评论(0编辑  收藏  举报