1. 添加依赖
<!-- freemarker 启动器的坐标 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
2. 编写视图
注意:springBoot 要求模板形式的视图层技术的文件必须要放到 src/main/resources 目录下一个名称为 templates 的文件夹下
<html>
<head>
<title>JSP</title>
<meta charset="UTF-8">
</head>
<body>
<table border="1" align="center" width="50%">
<tr><th>姓名</th><th>年龄</th></tr>
<#list userList as user>
<tr>
<td>${user.name}</td>
<td>${user.age}</td>
</tr>
</#list>
</table>
</body>
</html>
3. 创建 Controller
java bean:
public class User {
private String name;
private int age;
public User(String name, int age) {
this.name = name;
this.age = age;
}
}
controller:
@Controller
public class UserController {
@RequestMapping("/user")
public String getUser1( Model model){
List<User> list = new ArrayList<>();
list.add(new User("张三",20));
list.add(new User("李四",22));
list.add(new User("王五",24));
model.addAttribute("userList",list);
System.out.println(list.toString());
return "userList";
}
}
4. 创建启动器
@EnableAutoConfiguration
@ComponentScan("com.zth")
public class App {
public static void main(String[] args){
// 启动 SpirngBoot 项目
SpringApplication.run(App.class,args);
}
}
5. 访问结果
文件结构: