Velocity使用总结
一、载入Velocity依赖包
<dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity</artifactId> <version>1.7</version> </dependency>
二、Velocity类
package com.qunar.piao.data.integration.common; import java.io.ByteArrayInputStream; import java.io.CharArrayWriter; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.InputStream; import java.io.Reader; import java.io.Writer; import java.util.HashMap; import java.util.Map; import org.apache.velocity.VelocityContext; import org.apache.velocity.app.Velocity; import org.apache.velocity.context.Context; /** * Velocity引擎帮助类 * */ public class VelocityHelper { /** 单态实例 */ private static final VelocityHelper instance = new VelocityHelper(); /** 私有构造函数 */ private VelocityHelper() { // 初始化velocity的信息 主要设置一些Velocity的默认属性 // 初始化 try { Velocity.init(); } catch (Exception e) { e.printStackTrace(); } } /** * <pre> * 取得实例 * </pre> */ public static VelocityHelper getInstance() { return instance; } /** * <pre> * 渲染:从reader到writer * </pre> * * @param context * @param writer * @param reader * @return */ public boolean evaluate(Context context, Writer writer, Reader reader) { try { return Velocity.evaluate(context, writer, "", reader); } catch (Exception e) { throw new RuntimeException("velocity evaluate error! detail [" + e.getMessage() + "]"); } } /** * <pre> * 通过Map过滤一个输入流 * </pre> * * @param map * @param reader * @return */ @SuppressWarnings("unchecked") public InputStream evaluate(Map map, Reader reader) { try { // 把产生的输出流(字符流),转换成输入流(字节流) byte[] dataBytes = this.evaluateToWriter(map, reader).toString().getBytes(); return new ByteArrayInputStream(dataBytes); } catch (Exception e) { throw new RuntimeException("velocity evaluate error! detial [" + e.getMessage() + "]"); } } /** * <pre> * 通过Map过滤一个输入流 * </pre> * * @param map * @param reader * @return */ @SuppressWarnings("unchecked") public Writer evaluateToWriter(Map map, Reader reader) { try { VelocityContext context = convertVelocityContext(map); CharArrayWriter writer = new CharArrayWriter(); // 开始评估 this.evaluate(context, writer, reader); return writer; } catch (Exception e) { throw new RuntimeException("velocity evaluate error! detail [" + e.getMessage() + "]"); } } /** * <pre> * 取得Velocity系统属性 * </pre> * * @param key * @return */ public Object getProperty(String key) { return Velocity.getProperty(key); } /** * <pre> * 把Map转换成Context * </pre> */ private VelocityContext convertVelocityContext(Map<String, Object> map) { VelocityContext context = new VelocityContext(); if (map == null) { return context; } for (Map.Entry<String, Object> entry : map.entrySet()) { context.put(entry.getKey(), entry.getValue()); } return context; } /** * @param args * @throws FileNotFoundException */ public static void main(String[] args) throws FileNotFoundException { Map<String, Object> map = new HashMap<String, Object>(); map.put("date", "2011-11-21"); map.put("weather", "晴朗 李秋"); Reader reader = new FileReader("D:\\index.vm"); Writer writer = VelocityHelper.getInstance().evaluateToWriter(map, reader); // 今天是:2011-11-21,天气:晴朗 李秋! System.out.println(writer.toString()); } }
三、在D盘添加模版文件
今天是:${date},天气:${weather}!
四、运行VelocityHelper
今天是:2011-11-21,天气:晴朗 李秋!
五、Velocity语法总结
5.1 设置变量
#set( $test = $test2 + 3) #set( $test.a = "abc") #set( $test.b = 1)
5.2 循环
#foreach($conversionRate in $conversionRateList) $velocityCount //第几个,从1开始 #foreach($member in $conversionRate.entrySet()) $velocityCount //第几个,从1开始
$member.value //值 #end #end
5.3 if判断
#if ($foo < 10) Go North #elseif ($foo== 10) Go East #else Go West #end
5.4 字符串截取
$member.value.substring(0,1)
5.5 字符串长度
$!{str.length()}
参考:
http://velocity.apache.org/tools/releases/1.3/generic/
http://www.360doc.com/content/11/1203/22/834950_169480722.shtml