Velocity模板引擎试用
2013-11-07 19:10
134 查看
在框架处理日志的时候,支持配置方式生成日志。日志内容可以根据一定的规则来生成。这里就可以使用Velocity了。
下面针对常用的几种类型进行测试
1)map
Map map = new HashMap(); map.put("abc", "def"); context.put("param", map ); String commentEL = "$param.abc";
结果输出为def
2)dto
ServiceLogDto dto = new ServiceLogDto(); dto.setPartyCode("logCode"); context.put("param", dto ); commentEL = "$param.partyCode";
结果输出为logCode
3)[ ] 数组
ServiceLogDto [] dtos = new ServiceLogDto[2]; dtos[0]=dto; dtos[1]=new ServiceLogDto(); dtos[1].setPartyCode("testCode"); context.put("param", dtos ); commentEL = "#foreach($o in $param)$o.partyCode #end";
结果输出为 logCode testCode
4)list
List list = new ArrayList(); list.add(dto); ServiceLogDto dto2 = new ServiceLogDto(); dto2.setPartyCode("test2"); list.add(dto2); context.put("param", list ); commentEL = "#foreach($o in $param)$o.partyCode #end";
输出为 logCode test2
基本的类型都能用,即便有复杂的类型也可以在数据源这边转化一下。
附上上面代码片段的前后备用
VelocityEngine engine = new VelocityEngine(); engine.init(); VelocityContext context = new VelocityContext(); /////some code below String re= evaluate(engine, context, commentEL ); System.out.println(re); /////////////////////////////////////////////////////////////// private static String evaluate(VelocityEngine engine, VelocityContext context, String expr) { if (expr == null || expr.trim().length() == 0) { return ""; } try { CharArrayWriter writer = new CharArrayWriter(); engine.evaluate(context, writer, "", expr); return writer.toString(); } catch (Exception e) { return expr; } }
相关文章推荐
- IE9与Firefox4试用体验
- Reflector 试用过期后重置试用
- 实战试用 Android NDK 初见成效
- 试用新版Windows Home Server 2011 (Vail) 推荐
- php试用smarty和ADODB实现对数据分页读取
- scribefIre试用手记
- 国内几大Android应用市场试用小记——开发者篇
- 盗版 Win7 试用到期后黑屏咋办
- 怎样设计一个合理的软件试用机制或类似机制?
- Windows Live Writer试用
- jmeter试用记录-脚本录制
- 平行运算:Parallel.For、Parallel.Foreach的体验式试用
- debian虚拟机试用
- Office 365:申请试用
- IncrediBuild 30天试用过期破解清零工具 (转)
- ie6下面试用png使用滤镜需知
- windows azure 试用感受3
- 不试用include来包含文件。
- 习语言 中文编程软件 1.76版 正式发布了, 欢迎申请试用。
- Java 并发的初步学习 (Thread的 Runnable Callable 试用)