这篇文章给大家介绍如何在SpringBoot中整合freemarker,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
我们提供的服务有:成都网站建设、网站制作、微信公众号开发、网站优化、网站认证、梁平ssl等。为上千多家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的梁平网站制作公司
项目文件目录:

首先,pom.xml中导入freemarker的依赖:
org.springframework.boot spring-boot-starter-freemarker
在application.properties(或yml)配置文件中加入freemarker相关配置:
# freemarker静态资源配置 # 设定ftl文件路径 spring.freemarker.tempalte-loader-path=classpath:/templates # 关闭缓存,及时刷新,上线生产环境需要修改为true spring.freemarker.cache=false spring.freemarker.charset=UTF-8 spring.freemarker.check-template-location=true spring.freemarker.content-type=text/html spring.freemarker.expose-request-attributes=true spring.freemarker.expose-session-attributes=true spring.freemarker.request-context-attribute=request spring.freemarker.suffix=.ftl
这里指定了freemarker文件的路径是classpath/templates,在resources文件夹下的templates新建freemarker文件夹,并且在其中新建index.ftl(上面配置文件中已经指定了freemarker模板的文件后缀为ftl):
FreeMarker模板引擎 ${resource.name}
${resource.website}
${resource.language}
我们在resources下新建resource.properties:
com.haozz.opensource.name=wangshu com.haozz.opensource.website=www.haozz.top:18158/ com.haozz.opensource.language=chinese
在SpringBoot启动类统计目录下新建utils包,在其中新建Resources类(此处使用配置文件引入相关数据):
package com.haozz.freemarkerdemo.utils;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
//表示这个类是一个读取配置文件的类
@Configuration
//指定配置的一些属性,其中的prefix表示前缀
@ConfigurationProperties(prefix = "com.haozz.opensource")
//指定所读取的配置文件的路径
@PropertySource(value = "classpath:resource.properties")
public class Resource {
private String name;
private String website;
private String language;
//...setter and getter
}新建Controller包,新建FreeMarkerCtrl类:
package com.haozz.freemarkerdemo.controller;
import com.haozz.freemarkerdemo.utils.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping(value = "/ftl")
public class FreeMarkerCtrl {
@Autowired
private Resource resource;
@RequestMapping(value = "index")
public String index(ModelMap map){
map.addAttribute("resource",resource);
return "freemarker/index";
}
}这里的ModelMap就相当于SpringMVC中的ModelAndView,其中的很多方法也很类似,我们这里返回的字符串就是freemarker模板的路径,不用写后缀,因为配置文件中已经指定了后缀为.ftl
浏览器发起请求,得到结果:

这样,SpringBoot整合freemarker就好了。
我们再来试一下表格的形式。
FreeMarkerCtrl中新增方法:
@RequestMapping(value ="center")
public String center(ModelMap map){
map.put("users",parseUsers());
map.put("title","用户列表");
return "freemarker/center/center";
}
private List在resources/templates/freemarker下新建center文件夹,新建center.ftl:
${title}
| Name | Age | Phone |
|---|---|---|
| ${user.name} | ${user.age} | ${user.phone} |
浏览器请求:

可以看到,在center.ftl中,我们使用了<#list users as user>的写法,这个相当于jstl表达式中的c:forEach。而users集合我们在FreeMarkerCtrl已经初始化了。
关于如何在SpringBoot中整合freemarker就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。