美文网首页
Spring Boot + Thymeleaf

Spring Boot + Thymeleaf

作者: 请你吃糖 | 来源:发表于2018-07-06 09:46 被阅读17次

    1、导入pom

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

    2、Template

    在resource 目录下新建templates文件夹,新建user文件夹,新建list.html


    image.png

    html内容如下

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    <h1>用户列表</h1>
    <div>
        <ul>
            <li  th:each="user:${users}">
                <span th:text="${user.id}"></span>-
                <span th:text="${user.name}"></span>-
                <span th:text="${user.age}"></span>-
                <span th:text="${user.address}"></span>
            </li>
        </ul>
    </div>
    </body>
    </html>
    

    3、新建返回的UserDTO

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public class UserDTO {
        private Integer id;
        private String name;
        private Integer age;
        private String address;
    }
    

    4、Controller

    @Controller
    @RequestMapping("/user")
    public class UserController {
        @RequestMapping(value = "/list",method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
        public String getUser(Model model){
            List<UserDTO> userList = new ArrayList<>();
            for (int i = 0; i <10; i++) {
                userList.add(new UserDTO(i,"张三"+i,20+i,"上海"));
            }
            model.addAttribute("users", userList);
            return "/user/list";
        }
    }
    

    相关文章

      网友评论

          本文标题:Spring Boot + Thymeleaf

          本文链接:https://www.haomeiwen.com/subject/imkvuftx.html