2013-05-14 43 views
1

我使用的是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

爲什麼當視圖返回時不打印值?

EDIT ------------------------------------------- -----------------

這是工作代碼。

@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} 

回答

2

savePathcontextMap變量的屬性。

代替

至於訪問它會是context.get("savePath")的正確方法,:

${savePath} 

你應該使用:

${context.savePath} 
+0

感謝喜解釋,但它不工作。現在,當視圖返回exportComplete.vm ...時,結果是'$ {context.savePath}'。我的'exportCourse()'方法在不同的類中,這有什麼關係? – Basit 2013-05-14 12:20:29

+0

是的,這很重要。 ''exportCourse()'中的'context'與'handleRequest()'中的'context'不是相同的嗎?此外,'context'只有在你返回它時纔可用,也就是'else'情況:'返回新的ModelAndView(「templateScene/exportError」,「context」,context);' – acdcjunior 2013-05-14 12:22:36

+0

是的你是對的:)。我可以編輯你的答案,把工作代碼,然後我會接受你的答案,或者我編輯我的帖子,並把工作代碼,並接受你的答案。你說什麼 ? – Basit 2013-05-14 12:47:31