1. 使用maven构建SpringBoot的名叫spring-boot-view-freemarker项目
2. pom.xml?
<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>
<groupId>com.bjbs</groupId>
<artifactId>spring-boot-view-freemarker</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.13.RELEASE</version>
</parent>
<!-- 修改jdk版本 -->
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- springBoot的启动器 -->
<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>
<!-- <version>1.5.22.RELEASE</version> -->
</dependency>
</dependencies>
</project>
3. 新建User.java
package com.bjbs.pojo;
import java.io.Serializable;
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private String name;
private Integer age;
public User() {
}
public User(Integer id, String name, Integer age) {
this.id = id;
this.name = name;
this.age = age;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
4. 新建UserController.java
package com.bjbs.controller;
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.bjbs.pojo.User;
@Controller
public class UserController {
/*
* 处理请求, 产生数据
*/
@RequestMapping("/showUser")
public String showUser(Model model) {
List<User> list = new ArrayList<User>();
list.add(new User(1, "张三", 20));
list.add(new User(2, "李四", 22));
list.add(new User(3, "王五", 24));
// 需要一个Model对象
model.addAttribute("list", list);
// 跳转视图
return "userList";
}
}
5. 新建App.java启动类
package com.bjbs;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**springBoot整合Freemarker, 及其容易受到本地仓库混乱的jar包的影响。
* 错误如下:
* Whitelabel Error Page
*
* This application has no explicit mapping for /error, so you are seeing this as a fallback.
*
* Thu Dec 16 12:52:57 CST 2021
* There was an unexpected error (type=Not Found, status=404).
* No message available
*
* 我清空了本地仓库的所有jar, 重新运行本项目然后就成功了。
*/
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
6. SpringBoot要求模板形式的视图层技术的文件必须要放到src/main/resources目录下, 而且必须要一个名称为templates目录。
7. 编写userList.ftl?
<html>
<head>
<title>展示用户数据</title>
<meta charset="utf-8" />
</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.id}</td>
<td>${user.name}</td>
<td>${user.age}</td>
</tr>
</#list>
</table>
</body>
</html>
8. 启动项目, 并使用浏览器访问
?
|