簡體   English   中英

速度+彈簧

[英]Velocity + Spring

我正在嘗試使用上述組件設置一個Webapp。 除了最后一個障礙,我已經跳過了所有集成Spring&Velocity Tools的障礙。 我今天早上看到了這篇文章 ,並用與提供的答案稍有不同的答案對其進行了更新。 但是,一旦我嘗試將ParameterTool添加到我的模板之一中,如下所示:

#foreach( $key in $params.keySet() )
    $key = $params.getValue($key)
<br />
#end

我收到NPE java.lang.UnsupportedOperationException:請求為空。 必須先初始化ParameterTool! 根據我所讀的內容,這意味着該工具已正確配置,只是無法訪問該請求。 注意:我也收到接受的解決方案的錯誤。

有沒有人能夠在Spring上成功使用這些工具? 似乎是已知的缺陷,因為此Open Jira SPR-5514有一個Open Jira

對此問題“接受的答案 ”進行了稍微修改的版本可以解決此問題。

除了返回ViewContext,您還需要返回ViewToolContext。 您還需要准備工具箱,並根據需要在會話/請求中進行設置:

您將需要以所需的任何方式來初始化toolContext(在此處 ,我將提供關於如何使用更新的API進行此操作的答案,因為您將需要訪問ToolboxFactory。

修改后的createVelocityContext方法現在將需要以以下方式在創建ViewToolContext之前准備工具箱:

protected Context createVelocityContext(Map <String, Object> model, 
                                        HttpServletRequest request,
                                        HttpServletRespsone response) 
                                        throws Exception {

    initVelocityContext();  //Still keep toolContext static
                            //will need to also add this to 
                            //the servletContext -- left as an exercise
    prepareToolboxes(request, response);
    Context context = 
        new ViewToolContext(getVelocityEngine(), request, 
                                response, getServletContext());
    //Set model attrs to context
    ....
    return context;
}

private void prepareToolboxes(final HttpServletRequest request, 
                              final HttpServletResponse response) {
    String key = Toolbox.class.getName();
    if (factory.hasTools(Scope.REQUEST && request.getAttribute(key) == null) {
        Toolbox requestTools = factory.createToolbox(Scope.REQUEST);
        request.setAttribute(key, requestTools);
    }
    if (factory.hasTools(Scope.SESSION) {
       HttpSession session = request.getSession();
       synchronized(factory) {
           //Follow pattern from above
       }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM