Spring Boot 整合Freemarker
发表于|更新于
|阅读量:
FreeMarker 是一个模板引擎,一个基于模板生成文本输出的通用工具,使用纯 Java 编写,FreeMarker 被设计用来生成 HTML Web 页面,特别是基于 MVC 模式的应用程序,虽然 FreeMarker 具有一些编程的能力,但通常由 Java 程序准备要显示的数据,由FreeMarker 生成页面,通过模板显示准备的数据。符合MVC模式,采用哈希表存储,你可以专注于如何展现数据, 而在模板之外可以专注于要展示什么数据
<Excerpt in index | 首页摘要>
<The rest of contents | 余下全文>
整合Freemarker
创建maven项目,添加依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
|
编写视图
在templates/ 下建立 userList.ftl:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| <html> <head> <meta charset="utf-8"> <title>显示用户数据</title> </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>
|
创建Controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| package com.caicai.springbootviewfreemarker.controller;
import com.caicai.springbootviewfreemarker.pojo.Users; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping;
import java.util.ArrayList; import java.util.List;
@Controller public class UserController {
@RequestMapping("/showuser") public String showUser(Model model){ List<Users> list = new ArrayList<>(); list.add(new Users(1,"caicai",21)); list.add(new Users(2,"honghong",32)); list.add(new Users(3,"mingming",43)); model.addAttribute("list",list); return "userList";
} }
|
创建Uesrs类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| package com.caicai.springbootviewfreemarker.pojo;
public class Users { private Integer userid; private String username; private Integer userage;
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; }
public Users() { }
public Integer getUserid() { return userid; }
public void setUserid(Integer userid) { this.userid = userid; }
public Users(Integer userid, String username, Integer userage) { this.userid = userid; this.username = username; this.userage = userage; } }
|
创建启动类
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| package com.caicai.springbootviewfreemarker;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication public class Application {
public static void main(String[] args) { SpringApplication.run(Application.class, args); }
}
|