Spring Boot 集成freemarker模板引擎

本文正在参加「金石计划 . 瓜分6万现金大奖」

前言

J2EE的领域中包含5大引擎,分别为模板引擎、流程引擎、搜索引擎、规则引擎、报表引擎。每种引擎都能解决某一方面的问题,模板引擎解决的是用户界面与业务数据分离,流程引擎解决的是驱动业务按照一定的流程执行,搜索引擎解决的是用户的检索问题,规则引擎解决的是不同的业务决策需要从代码中进行分离。关于这些引擎技术,在后续的文章中会逐一进行重点讲解。

图片.png

本文将讲解模板引擎,J2EE常用的模板引擎有Velocity、Freemark、Thymeleaf等,由于Velocity模板引擎在Spring Boot2.0之后已经不再支持,所以本文重点讲解Freemar模板引擎。

简介

FreeMarker是一个用Java语言编写的模板引擎,它基于模板来生成文本输出。将业务数据和用户界面的显示进行分离,专注于视图的渲染,从而提高效率。

应用场景

  • 视图渲染
  • 制作模板,例如邮件模板、短信模板等。
  • 代码生成器,例如mybatis-plus-generator
  • 导出word文档

基础集成

引入jar包

<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>
复制代码

模板属性配置

# freemarker静态资源配置
spring:
  freemarker:
    tempalte-loader-path: classpath:/templates
    cache: true
    charset: UTF-8
    check-template-location: true
    content-type: text/html
    expose-request-attributes: true
    expose-session-attributes: true
    request-context-attribute: request
    suffix: .ftl
复制代码

-说明:

  • tempalte-loader-path:指定模板文件的路径。
  • cache:文件是否缓存
  • charset:设置文件的字符编码
  • content-type:文件的类型
  • suffix:文件后缀
  • check-template-location: 检查模板文件是否存在
  • expose-request-attributes:是否开启request属性,默认为false
  • expose-session-attributes: 是否开启HttpSession属性

业务实现

@Controller
public class UserController
{
    @RequestMapping("/user")
    public String showUser(Model model) 
    {
        List<User> users = new ArrayList<>();
        for (int i = 0; i < 10; i++) 
        {
            User user = new User();
            user.setId((long) i);
            user.setUsername("剑圣无痕" + i);
            user.setAddress("广东省人民路" + i+"号");
            users.add(user);
        }
        model.addAttribute("users", users);
        return "user";
    }
}

复制代码

视图文件

<table border="1">
    <tr>
        <td>用户编号</td>
        <td>用户名称</td>
        <td>用户地址</td>
    </tr>
    <#list users as user>
        <tr>
            <td>${user.id}</td>
            <td>${user.username}</td>
            <td>${user.address}</td>
        </tr>
    </#list>
</table>
复制代码

说明:关于freemark的基础的循环、条件等语法,本文将不进行讲解,详情大家可以查看官网

运行结果

图片.png

其他特性

通过上述的步骤,我们实现了Spring Boot集成Freemark的视图渲染功能,但是在实际的项目中需要实现模板设定、导出word文档等功能,那么通过Freemark如何实现呢?

加载特定模板

 public void sendFreemarkTemplateMail(String templateName,Mail mail)
    {
        HashMap<String, Object> map = new HashMap<>();
        map.put("companyName", "广东剑圣无痕股份有限公司");
        map.put("address", "广东省建设路1008号");
        map.put("phone", "13123456789");
        Template template;
        try
        {
            template = configurer.getConfiguration().getTemplate(templateName);
            String context = FreeMarkerTemplateUtils.processTemplateIntoString(template, map);
            mail.setBody(context);
            sendHtmlMail(mail);
        }
        catch (Exception e)
        {
           logger.error("send freemark error",e);
        }
    }
复制代码

上述是发送邮件模板设定,通过freemark加载模板文件输出内容进行邮件发送

总结

本文对于Spring Boot集成Freemark进行了相关讲解,如有疑问请及时反馈。

猜你喜欢

转载自juejin.im/post/7166455005770153991
今日推荐