spring mvc 整合freemark 生成邮件模板

Spring boot配置使用Freemarker 请参考这篇博客点击进入

  1. 添加pom
  2. 添加配置
  3. 测试类

    一.添加pom

<dependency>
    <groupId>org.freemarker</groupId>
    <artifactId>freemarker</artifactId>
    <version>2.3.25-incubating</version>
</dependency>

###二.添加配置
1.在resource 目录下创建emailtemplates目录
2.在配置文件中添加

<bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
        <property name="templateLoaderPath" value="classpath:emailtemplates"/><!--指定模板文件目录-->
        <property name="freemarkerSettings"><!-- 设置FreeMarker环境属性-->
            <props>
                <!--<prop key="template_update_delay">1800</prop>&lt;!&ndash;刷新模板的周期,单位为秒&ndash;&gt;-->
                <prop key="default_encoding">UTF-8</prop><!--模板的编码格式 -->
                <prop key="locale">zh_CN</prop><!-- 本地化设置-->
            </props>
        </property>
    </bean>

###三.测试类
1.freemarker模板


<html>
<body>
<h3>【${operationLevel}】${title}</h3>

<table>
    <tbody>
    <tr>
        <td style="width: 150px" class="text-right">项目:</td>
        <td>${projectName }</td>
    </tr>
    <tr>
        <td style="width: 150px" class="text-right">中间件类型:</td>
        <td>${type }</td>
    </tr>
    <tr>
        <td style="width: 150px" class="text-right">操作类型:</td>
        <td>${operationType }</td>
    </tr>
    <tr>
        <td style="width: 150px" class="text-right">操作用户:</td>
        <td>${operator}</td>
    </tr>
    <tr>
        <td style="width: 150px" class="text-right">IP:</td>
        <td>${ip}</td>
    </tr>
    <tr>
        <td style="width: 150px" class="text-right">操作时间:</td>
        <td>${operationDate}</td>
    </tr>
    <tr>
        <td style="width: 150px" class="text-right">操作对象:</td>
        <td>${operationObject}</td>
    </tr>
    <tr>
        <td style="width: 150px" class="text-right">操作上下文:</td>
        <td>${operationContext}</td>
    </tr>
    <tr>
        <td style="width: 150px" class="text-right">描述:</td>
        <td>${describe}</td>
    </tr>
    </tbody>
</table>
</body>
</html>

2.执行类

@Autowired
private FreeMarkerConfigurer freeMarker;

 /**
 * 得到邮件内容
 *
 * @param noticeTemplateVo
 * @return
 */
private String getEmailContent(NoticeTemplateVo noticeTemplateVo) {
    String htmlText = null;
    try {
        // 通过指定模板名获取FreeMarker模板实例
        Template template = freeMarker.getConfiguration().getTemplate("emailTemplate.html");

        // FreeMarker通过Map传递动态数据
        Map<String, String> map = new HashMap<String, String>();
        map.put("projectName", noticeTemplateVo.getProjectName());
        map.put("type", noticeTemplateVo.getType());
        map.put("title", noticeTemplateVo.getTitle());
        map.put("operator", noticeTemplateVo.getOperator());
        map.put("operationLevel", noticeTemplateVo.getOperationLevel());
        map.put("ip", noticeTemplateVo.getIp());
        map.put("operationDate", noticeTemplateVo.getOperationDate());
        map.put("operationType", noticeTemplateVo.getOperationType());
        map.put("operationObject", noticeTemplateVo.getOperationObject());
        map.put("operationContext", noticeTemplateVo.getOperationContext());
        map.put("describe", noticeTemplateVo.getDescribe());

        // 解析模板并替换动态数据,最终content将替换模板文件中的${content}标签。
        htmlText = FreeMarkerTemplateUtils.processTemplateIntoString(template, map);

    } catch (Exception e) {
        logger.error("Exception occured while processing fmtemplate:" + e.getMessage(), e);
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值