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>
调用 对象的方法,需要 $!
输出结果: