速度 + 弹簧 [英] Velocity + Spring

查看:22
本文介绍了速度 + 弹簧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用上述组件设置 web 应用程序.除了整合 Spring & 的最后一道障碍之外,我已经跳过了所有障碍.速度工具.我今天早上看到了这篇文章,并用与提供的答案略有不同.但是,一旦我尝试添加 ParameterTool 到我的模板之一,如下所示:

I am attempting to setup a webapp with the above components. I've jumped all but the last hurdle which is integrating 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:

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

我收到 NPE java.lang.UnsupportedOperationException: Request is null.ParameterTool 必须先初始化!根据我读过的内容,这意味着工具配置正确,只是它无权访问请求.注意:我也收到了已接受解决方案的错误.

I receive a NPE java.lang.UnsupportedOperationException: Request is null. ParameterTool must be initialized first! 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.

有没有人成功地将这些工具与 Spring 一起使用?似乎这是一个已知的缺陷,因为此 Open Jira SPR-5514

Has anyone successfully been able to use these tools with Spring? Seems that it's a known deficiency as there is an Open Jira for this Open Jira SPR-5514

推荐答案

接受的答案来自这个问题解决了这个问题.

您需要返回一个 ViewToolContext,而不是返回 ViewContext.您还需要准备工具箱并根据需要在会话/请求中设置它们:

Instead of returning a ViewContext you need to return a ViewToolContext. You will also need to prepare the toolboxes and set them on the session / request as applicable:

您需要以您需要的任何方式初始化 toolContext(查看我提供的答案 此处 了解如何使用更新的 API 执行此操作,因为您将需要访问 ToolboxFactory.

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.

修改后的 createVelocityContext 方法现在需要在以下列方式创建 ViewToolContext 之前准备工具箱:

The modified createVelocityContext method will now need to prepare the toolboxes prior to creating the ViewToolContext in the following manner:

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

这篇关于速度 + 弹簧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆