Freemarker与SpringBoot集成
一、Freemarker概述
- FreeMarker是一个用Java语言编写的模板引擎,它基于模板来生成文本输出。FreeMarker与Web容器无关,即在Web运行时,它并不知道Servlet或HTTP。它不仅可以用作表现层的实现技术,而且还可以用于生成XML,JSP或Java 等。
- freemarker的功能:
- 根据模板问题动态渲染html文件(动态塞入后端提供的数据)
- 根据模板生成各种格式的文件(生成全新的实体文件)
二、使用Freemarker动态渲染html文件
搭建演示环境
新建springboot项目,添加pom依赖(web + thymeleaf + freemarker)
pom.xml<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</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>
配置文件
application.propertes:# THYMELEAF (ThymeleafAutoConfiguration) spring.thymeleaf.cache=false spring.thymeleaf.enabled=true spring.thymeleaf.encoding=UTF-8 spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html spring.thymeleaf.content-type=text/html spring.thymeleaf.mode=LEGACYHTML5 spring.thymeleaf.check-template-location=true ##freemarker spring.freemarker.cache=false spring.freemarker.charset=UTF-8 spring.freemarker.check-template-location=true spring.freemarker.content-type=text/html spring.freemarker.enabled=true spring.freemarker.suffix=.ftl spring.freemarker.template-loader-path=classpath:/templates/freemarker
模板文件demo.ftl
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> Hello World! --Name:${name} </body> </html>
使用Freemarker动态渲染演示
主要有如下几种类型的使用:String、Object、Map、List(包括取下标)、时间格式化、null判断;String类型
Java代码:@RequestMapping("string/{name}") public String demo(@PathVariable String name, Model model){ model.addAttribute("name",name); return "freemarker/demo"; }
效果图:
对象类型
Java代码:
新增Person类:public class Person { private Integer id; private String name; (setter、getter、 ···) }
FreemarkerController.java
@RequestMapping("obj") public String fmObj( Model model){ model.addAttribute("person",new Person(1,"tony")); return "freemarker/demo"; }
改造模板文件demo.ftl:
<body> <#--Hello World! --Name:${name}--> Hello World! --Name:${person.name} </body>
效果图:
List类型
@RequestMapping("list") public String fmList( Model model){ List<Person> personList = new ArrayList<>(); for (int i = 0; i < 10; ) { personList.add(new Person(i+10,"张三"+ ++i)); } model.addAttribute("personList",personList); return "freemarker/demo"; }
改造模板文件demo.ftl:
<body> <#--Hello World! --Name:${name}--> <#--Hello World! --Name:${person.name}--> <#list personList as person > <h2>索引:${person_index}, 编号:${person.id}, 姓名:${person.name},Hello World!</h2> </#list> </body>
效果图:
Map类型:
Java代码:@RequestMapping("map") public String fmMap( Model model){ Map<String,Person> personMap = new HashMap<>(); for (int i = 0; i < 10; ) { personMap.put("key"+i,new Person(i+10,"张三"+ ++i)); } model.addAttribute("personMap",personMap); return "freemarker/demo"; }
改造模板文件:
<body> <#--Hello World! --Name:${name}--> <#--Hello World! --Name:${person.name}--> <#--<#list personList as person > <h2>索引:${person_index}, 编号:${person.id}, 姓名:${person.name},Hello World!</h2> </#list>--> <#list personMap?keys as key > <h2>编号:${personMap[key].id}, 姓名::${personMap[key].name},Hello World!</h2> </#list> </body>
效果图:
时间格式:
Java代码:
@RequestMapping("date") public String fmDate( Model model){ model.addAttribute("date",new Date()); return "freemarker/demo"; }
改造模板文件:
<h1>日期:${date?date}</h1> <h1>时间:${date?time}</h1> <h1>日期+时间:${date?datetime}</h1> <h1>自定义时间格式:${date?string("yyyy-MM/dd HH:mm:ss")}</h1>
效果图:
null判断
Java代码:@RequestMapping("null") public String fmNull( Model model){ model.addAttribute("notNullKey","haha"); model.addAttribute("nullKey",null); return "freemarker/demo"; }
改造模板文件:
<h1>非null:${notNullKey}</h1> <h1>null:${nullKey!}(相当于${nullKey!""})</h1> <h1>wasd:${wasd!"abcdefg"}</h1>
效果图:
三、使用Freemarker生成实体文件
- (未完。。。)
代码发布于github:https://github.com/ZephyrLai/SpringBoot-BasicModules/tree/master/springboot-freemarker