我正在使用带有 Velocity 的 Spring,我尝试在我的速度模板上打印一些文字,但它不起作用。这是我的模板,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;
}
}
${savePath}
当视图返回exportComplete.vm时,我得到了结果。
为什么视图返回时不打印值?
编辑 - - - - - - - - - - - - - - - - - - - - - - - - - ------------
这是工作代码。
@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}