Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 属性值未在velocity模板上打印_Spring_Spring Mvc_Velocity - Fatal编程技术网

Spring 属性值未在velocity模板上打印

Spring 属性值未在velocity模板上打印,spring,spring-mvc,velocity,Spring,Spring Mvc,Velocity,我将Spring与Velocity一起使用,并尝试在Velocity模板上打印一些文字,但它不起作用。这是我的模板,exporteComplete.vm: ${savePath} 代码如下: @Override public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { .... boolean success =

我将Spring与Velocity一起使用,并尝试在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;        
    }
}
这里是方法

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;

    }

}

savePath
context
Map
变量的属性

正确的访问方法是
context.get(“savePath”)
,而不是:

${savePath}
你应使用:

${context.savePath}

嗨,谢谢你的解释,但它不起作用。现在,当视图返回exportComplete.vm时,结果是
${context.savePath}
。我的
exportCourse()
方法在不同的类中,这有关系吗?是的,有关系。
exportCourse()
中的
context
handleRequest()中的
context
不同?另外,
context
仅在您返回它时可用,即
else
案例:
returnnewmodelandview(“templateScene/exportError”,“context”,context)是,您是对的:)。我可以编辑你的答案,把工作代码,然后我会接受你的答案,或者我编辑我的帖子,把工作代码放在那里,接受你的答案。你怎么说?如果你编辑我的答案,评审员会拒绝。在“编辑:”之类的内容之后添加到您的问题中,我将相应地更新答案,以供将来的观众使用。谢谢:)我想问的一件事是,不必调用
VelocityEngineUtils.mergeTemplateIntoString(引擎,“templateScene/exportComplete.vm”,“UTF-8”,上下文)。无论您在上下文中放置什么,它都会在模板上自动呈现。我说得对吗?
${savePath}
${context.savePath}