添加依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency>
创建模板文件
---保存位置resources/templates 目录下 文件后缀名.ftl
<html> <head> <title>spring boot</title> </head> <body> <table border="1px"> <thead> <tr> <th>id</th> <th>用户名</th> <th>密码</th> <th>姓名</th> </tr> </thead> <tbody> <#list userList as user> <tr> <td>${user.id}</td> <td>${user.username}</td> <td>${user.password}</td> <td>${user.name}</td> </tr> </#list> </tbody> </table> </body> </html>
编写application.yml配置文件
spring:
datasource:
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/springboot
username: root
password: root
jpa:
database: MySQL
show-sql: true
generate-ddl: true
page:
rows: 50
编写Controller,将结果传给模板
@Controller public class PageController { @Autowired private UserDao userDao; @Value("${page.rows}") private Integer rows; @RequestMapping("/page/user/list") public String showUserList(Model model){ List<User> userList=userDao.findAll(); model.addAttribute("userList",userList); return "user"; } @RequestMapping("/page/rows") @ResponseBody public Map showRows(){ Map map = new HashMap(); map.put("rows",rows); return map; } }