Spring boot配置使用Freemarker

在 Spring boot下配置使用Freemarker

1. Spring boot 的配置文件

在application-dev.properties中添加模板地址

#freemark
freeMarker.templateLoaderPath=classpath:mailTemplate

并且在src下建mailTemplate目录(后续再改成从数据库获得)

2. Freemarker 的配置文件

FreemarkerConfig.java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean;


@Configuration
public class FreemarkerConfig
{
    @Autowired
    private Environment env;

    @Bean(value = "freemarkerConfiguration")
    public FreeMarkerConfigurationFactoryBean getFreeMarkerConfigurationFactoryBean()
    {
        FreeMarkerConfigurationFactoryBean freeMarkerConfigurationFactoryBean = new FreeMarkerConfigurationFactoryBean();
        String templateLoaderPath = env.getProperty("freeMarker.templateLoaderPath");
        freeMarkerConfigurationFactoryBean.setTemplateLoaderPath(templateLoaderPath);
        return freeMarkerConfigurationFactoryBean;
    }
}

Environment 慎用,最好使用@Value(“${freeMarker.templateLoaderPath}”) 这种形式获取资源变量

3. FreeMarker模板

建在mailTemplate目录下 template.html

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>

<body>
    <table>
        <tbody>
            <tr>
                <td style="width: 150px" class="text-right">ID:</td>
                <td>${id }</td>
            </tr>
            <tr>
                <td style="width: 150px" class="text-right">用户:</td>
                <td>${createUser}</td>
            </tr>
            <tr>
                <td style="width: 150px" class="text-right">时间:</td>
                <td>${createDate}</td>
            </tr>           
        </tbody>
    </table>
</body>

</html>

4. 使用方法

templateName=”template.html”
model是模板类中的键值对
例:

Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", mailMapDto.getFeedback().getId());
        map.put("createUser", mailMapDto.getFeedback().getUsername() + "("
                + mailMapDto.getFeedback().getCreateUser() + ")");
        map.put("createDate", mailMapDto.getFeedback().getCreateDate());

执行方法:

public String geFreeMarkerTemplateContent(String templateName,
            Map<String, Object> model) {
        StringBuffer content = new StringBuffer();
        try {
            content.append(FreeMarkerTemplateUtils.processTemplateIntoString(
                    freemarkerConfiguration.getTemplate(templateName), model));
            return content.toString();
        } catch (Exception e) {
            logger.error(
                    "Exception occured while processing fmtemplate:"
                            + e.getMessage(), e);
        }
        return "";
    }

这样就获得拼装好的字符串了

后续在优化成数据库获得模板的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值