Velocity 把工具类返回到前端并调用工具类方法

来源:互联网 发布:吉利知豆d1导航系统 编辑:程序博客网 时间:2024/06/03 11:51

Velocity 把工具类返回到前端并调用工具类方法,本例是转换date类型

1.java代码:


Map<String, Object> interviewMap = new HashMap<String, Object>();
interviewMap.put("date_", new Date());
interviewMap.put("name", "哈哈");
VelocityContext context = new VelocityContext();
DateFormatUtils dateTool = new DateFormatUtils();// 转换日期的工具类
context.put("dateTool", dateTool);
context.put("interviewMap", interviewMap);
StringWriter body = new StringWriter();
Velocity.mergeTemplate("template/test.html", "UTF-8", context, body);
String content = body.toString();




html代码:

<p>Email: ${interviewMap.name}</p></p>
<p>date1: $!dateTool.format(${interviewMap.date_}, "yyyy")</p>


调用 对象的方法,需要  $!  


输出结果: