我正在使用Spring with Velocity,我尝试在我的力度模板上打印一些文字,但它不起作用。这是我的模板, exporteComplete.vm:
${savePath}
以下是代码:
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
....
boolean success = processor.exportCourse(courseId, exportPlayer, exportAssets, exportJson);
...
if (success) {
log.debug("Export Success");
return new ModelAndView("templateScene/exportComplete");
} else {
log.debug("Export Failure");
return new ModelAndView("templateScene/exportError", "context", context);
}
}
以下是方法:
public boolean exportCourse(String courseId, boolean exportPlayer, boolean exportAssets, boolean exportJson) {
context = new HashMap<Object, Object>();
context.put("savePath", "save path complete");
VelocityEngineUtils.mergeTemplateIntoString(engine, "templateScene/exportComplete.vm", "UTF-8", context);
boolean test = true;
if (test) {
return true;
}
}
当视图返回 exportComplete.vm 时,我最终得到${savePath}
结果。
视图返回时为什么不打印值?
修改 -------------------------------------------------- ----------
这是有效的代码。
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
....
context = new HashMap<Object, Object>();
boolean success = processor.exportCourse(courseId, exportPlayer, exportAssets, exportJson, context);
if (success) {
log.debug("Export Success");
return new ModelAndView("templateScene/exportComplete", "context", context);
} else {
log.debug("Export Failure");
return new ModelAndView("templateScene/exportError", "context", context);
}
}
这是方法
public boolean exportCourse(String courseId, boolean exportPlayer, boolean exportAssets, boolean exportJson, Map<Object, Object> myContext) {
...
if (myContext != null) {
myContext.put("savePath", "Save Path Complete");
return true;
}
}
这是模板
${context.savePath}
答案 0 :(得分:2)
savePath
是context
Map
变量的属性。
正确的访问方式是context.get("savePath")
,而不是:
${savePath}
您应该使用:
${context.savePath}