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)测试