简体   繁体   English

速度+弹簧

[英]Velocity + Spring

I am attempting to setup a webapp with the above components. 我正在尝试使用上述组件设置一个Webapp。 I've jumped all but the last hurdle which is integrating Spring & Velocity Tools. 除了最后一个障碍,我已经跳过了所有集成Spring&Velocity Tools的障碍。 I saw this post this morning, and updated it with a slightly different answer than what was provided. 我今天早上看到了这篇文章 ,并用与提供的答案稍有不同的答案对其进行了更新。 However, once I attempted to add in ParameterTool to one of my templates like so: 但是,一旦我尝试将ParameterTool添加到我的模板之一中,如下所示:

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

I receive a NPE java.lang.UnsupportedOperationException: Request is null. 我收到NPE java.lang.UnsupportedOperationException:请求为空。 ParameterTool must be initialized first! 必须先初始化ParameterTool! According to what I've read that means that the tooling was configured properly, just that it doesn't have access to the Request. 根据我所读的内容,这意味着该工具已正确配置,只是无法访问该请求。 Note: I receive the error with the accepted solution as well. 注意:我也收到接受的解决方案的错误。

Has anyone successfully been able to use these tools with Spring? 有没有人能够在Spring上成功使用这些工具? Seems that it's a known deficiency as there is an Open Jira for this Open Jira SPR-5514 似乎是已知的缺陷,因为此Open Jira SPR-5514有一个Open Jira

A slightly modified version of The accepted answer from this question resolves this issue. 对此问题“接受的答案 ”进行了稍微修改的版本可以解决此问题。

Instead of returning a ViewContext you need to return a ViewToolContext. 除了返回ViewContext,您还需要返回ViewToolContext。 You will also need to prepare the toolboxes and set them on the session / request as applicable: 您还需要准备工具箱,并根据需要在会话/请求中进行设置:

You will need to initialize the toolContext in whichever manner you need (look at my provided answer here on how to do this with the updated APIs, as you're going to need access to the ToolboxFactory. 您将需要以所需的任何方式来初始化toolContext(在此处 ,我将提供关于如何使用更新的API进行此操作的答案,因为您将需要访问ToolboxFactory。

The modified createVelocityContext method will now need to prepare the toolboxes prior to creating the ViewToolContext in the following manner: 修改后的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