FreeMarker,,Velocity解析工具类的模板应用

本文介绍如何利用FreeMarker模板引擎结合Spring框架处理邮件和短信中的动态内容。通过配置FreeMarkerConfigurer并使用特定方法实现HTML邮件模板的动态数据替换。

工程中使用了邮件和短信模板,,

 

内容使用的html代码, vm文件。

 

可以使用Spring 的解析工具解析,替换其中的动态数据 。

 

FreeMarker解析:

 

	@Autowired
	private FreeMarkerConfigurer freeMarkerConfigurer;


   public String getFtlText(Map data,String ftl){   
       String htmlText=null;
        
       Template tpl    = null;   
       try {   
           tpl =  freeMarkerConfigurer.getConfiguration().getTemplate(ftl);//加载资源文件   
           htmlText = FreeMarkerTemplateUtils.processTemplateIntoString(tpl, data);//加入map到模板中 对应${content}   
       } catch (IOException e) {              
           e.printStackTrace();   
       } catch (TemplateException e) {            
           e.printStackTrace();   
       }   
       return htmlText;           
   }  

 

注: 其中的 freeMarkerConfigurer 可以在Spring中配置如下:

 

	<bean id="freeMarkerConfigurer"  
        class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">   
        <property name="templateLoaderPaths"  
            value="classpath:template/sms" />   
        <property name="freemarkerSettings">   
            <props>   
                <prop key="template_update_delay">1800</prop><!-- 模板更新延时 -->   
                <prop key="default_encoding">UTF-8</prop>   
                <prop key="locale">zh_CN</prop>   
            </props>   
        </property>   
    </bean>
    
 

 

 

 

 

VM 文件 :

 

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>处理中</title>
</head>
<body>
<div style="background:#F6F5F6;padding:5px;width:630px;font-size:12px;color:#2D2D2D;font-family:tahoma">
	<table style="background:#fff;width:630px;border-spacing:0;border-collapse:collapse;font-size:12px;color:#2D2D2D;font-family:tahoma">
		<tr>
			<td style="background:#FEEAC2;height:32px;line-height:32px;padding:0;">
				<table style="width:100%">
					<tr>
.................此处几乎不使用图片,且样式全部套在标签内.................
....................................
..................................
<tr>
						<td style="padding-bottom:10px;">很高兴通知您以下订单已经成功提交: </td>
					</tr>
					<tr>
						<td style="padding:5px 0;font-weight:bold;">订单详情</td>
					</tr>
					<tr>
						<td style="padding:5px 0;">城市: [${hotelOrder.cityName}]</td>
					</tr>
					<tr>
						<td style="padding:5px 0;">酒店名称: ${hotelOrder.hotelName}</td>
					</tr>
					<tr>
						<td style="padding:5px 0;">入住日期:${hotelOrder.strEnterDate}</td>
					</tr>
					<tr>
						<td style="padding:5px 0;">入住天数: ${hotelOrder.leaveDay} </td>
					</tr>

.................................................
.................................................
.................................................

 

 

解析的方法: 

 

	data.put("module", 2);
	data.put("orderNo", orderNo);
	data.put("email", email);

	data.put("hotelOrder", hotelOrder);
	
	getContent(data,templateId + ".vm");
	
	

	public String getContent(Map dataMap,String vmString) {
		String body = getBody(dataMap, vmString);
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");
		try {
			response.getWriter().append(body);
			response.flushBuffer();	
		} catch (IOException e) {
			e.printStackTrace();
		}
		return NONE;
	} 
	
	public String getBody(Map<String, Object> dataMap, String tpl) {
		String body = VelocityEngineUtils.mergeTemplateIntoString(
				velocityEngine, tpl, dataMap);
		return body;
	}
	

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值