首页 > 其他分享 >SpringBoot整合Thymeleaf

SpringBoot整合Thymeleaf

时间:2024-06-11 11:23:10浏览次数:12  
标签:category1 RequestMapping getAllCategory Thymeleaf 整合 SpringBoot

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

参考

标签:category1,RequestMapping,getAllCategory,Thymeleaf,整合,SpringBoot
From: https://www.cnblogs.com/litayun/p/18241742

相关文章