其他分享
首页 > 其他分享> > SpringBoot - 06整合Freemarker

SpringBoot - 06整合Freemarker

作者:互联网

SpringBoot - 06整合Freemarker

(1)修改POM文件,添加Freemarker启动器

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

(2)创建实体类

public class User {
    private String userName;
    private String sex;
    private Integer age;
    // 省略其他代码
}

(3)创建Controller

@Controller
public class UserController {

    @GetMapping("/showUsers")
    public String showUsers(Model model){
        List<User> listUser = new ArrayList<>();
        listUser.add(new User("admin","F",23));
        listUser.add(new User("root","M",25));
        listUser.add(new User("app","F",27));
        listUser.add(new User("mali","M",28));
        model.addAttribute("list",listUser);
        return "userList";
    }
}

(4)创建视图  resources/templates/userList.ftl

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <table border="1" align="center" width="50%">
        <tr>
            <th>Name</th>
            <th>Sex</th>
            <th>Age</th>
        </tr>

        <#list list as user>
            <tr>
                <td>${user.userName}</td>
                <td>${user.sex}</td>
                <td>${user.age}</td>
            </tr>
        </#list>
    </table>
</body>
</html>

(5)修改配置文件

spring.freemarker.suffix=.ftl

(6)测试

 

标签:06,SpringBoot,Freemarker,private,listUser,add,User,new,String
来源: https://www.cnblogs.com/kingdomer/p/14130417.html