spring mvc 整合freemark 生成邮件模板

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

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

    一.添加pom

<dependency>
    <groupId>org.freemarkergroupId>
    <artifactId>freemarkerartifactId>
    <version>2.3.25-incubatingversion>
dependency>

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

<bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
        <property name="templateLoaderPath" value="classpath:emailtemplates"/>
        <property name="freemarkerSettings">
            <props>
                
                <prop key="default_encoding">UTF-8prop>
                <prop key="locale">zh_CNprop>
            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;
}

你可能感兴趣的:(spring)