SpringBoot整合Freemarker
1.修改pom文件,添加坐标freemarker启动器坐标
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
</parent>
<groupId>com.dxh</groupId>
<artifactId>09-spring-boot-view-freemarker</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- 修改JDK版本 -->
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- Spring Boot 的启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- freemarker启动器坐标 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
</dependencies>
</project>
2.编写视图:SpringBoot要求模板形式的视图层技术的文件必须放在src/main/resources/目录下必须要一个名称为:templates目录。 视图名为userList.ftl
<html>
<head>
<title>展示用户数据</title>
<meta charset="utf-8"></meta>
</head>
<body>
<table border="1" align="center" width="50%">
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</th>
</tr>
<#list list as user>
<tr>
<td>${user.userId}</td>
<td>${user.userName}</td>
<td>${user.userAge}</td>
</tr>
</#list>
</table>
</body>
</html>
3.创建controller、POJO
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.dxh.pojo.Users;
/**
* SpringBoot整合jsp
*
*/
@Controller
public class UserController {
/**
* 处理请求,产生数据
*/
@RequestMapping("/showUser")
public String showUser(Model model) {
List<Users> list = new ArrayList<>();
list.add(new Users(1,"张三",20));
list.add(new Users(2,"李四",22));
list.add(new Users(3,"赵武",25));
//需要一个Model对象
model.addAttribute("list",list);
//跳转视图
return "userList";
}
}
package com.dxh.pojo;
public class Users {
private Integer userId;
private String userName;
private Integer userAge;
public Users(Integer userId, String userName, Integer userAge) {
super();
this.userId = userId;
this.userName = userName;
this.userAge = userAge;
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public Integer getUserAge() {
return userAge;
}
public void setUserAge(Integer userAge) {
this.userAge = userAge;
}
}
4.创建启动器——运行