速度 +春天

发布于 2024-10-18 02:13:46 字数 807 浏览 6 评论 0原文

我正在尝试使用上述组件设置一个网络应用程序。除了最后一个障碍(集成 Spring 和 Spring)之外,我已经跨越了所有障碍。速度工具。我今天早上看到了这篇文章,并用答案与提供的略有不同。但是,一旦我尝试添加 ParameterTool 到我的模板之一,如下所示:

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

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

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

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

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

强辩 2024-10-25 02:13:46

接受的答案的稍微修改版本< 此问题中的 /a> 解决了此问题。

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

您将需要以您需要的方式初始化 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
       }
    }
}

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

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
       }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文