Spring 3应用程序中的Velocity-Tools - Linktool导致NPE,如何解决?

发布于 2024-12-04 19:18:18 字数 2344 浏览 2 评论 0原文

我正在使用修改后的 VelocityToolboxView(在 stackoverflow 上找到)来利用 spring 3 中的 Velocity-Tools 2.0。 看起来它配置得很好,但是当我在 .vm 文件中调用 $link 工具时,我得到了一个 NPE。扫描 Velocity-Tools 源,我发现它尝试使用来自 ValueParser 属性的请求和响应来配置该工具,但它们在这里为空。

这里是堆栈:

LinkTool.configure(ValueParser) line: 100   
LinkTool(SafeConfig).configure(Map) line: 113   
NativeMethodAccessorImpl.invoke0(Method, Object, Object[]) line: not available [native method]  
NativeMethodAccessorImpl.invoke(Object, Object[]) line: 39  
DelegatingMethodAccessorImpl.invoke(Object, Object[]) line: 25  
Method.invoke(Object, Object...) line: 597  
ToolInfo.invoke(Method, Object, Object) line: 363   
ToolInfo.configure(Object, Map<String,Object>) line: 294    
ToolInfo.create(Map<String,Object>) line: 255   
Toolbox.getFromInfo(String, String, Map<String,Object>) line: 152   
Toolbox.get(String, String, Map<String,Object>) line: 112   
ToolContext.findTool(String) line: 221  
ToolContext.get(String) line: 206   
VelocityContext(AbstractContext).get(String) line: 197  

当 ValueParser 此时需要在其映射中包含请求/响应值时,通常在哪里注入以及由谁注入?

这是我使用的视图类:

public class VelocityToolsView extends VelocityToolboxView
{

  private static ToolContext toolContext;

  @Override
  protected Context createVelocityContext(@SuppressWarnings("rawtypes") Map model,
      HttpServletRequest request, HttpServletResponse response) throws IOException
  {
    VelocityContext context = new VelocityContext(getToolContext());
    if (model != null)
    {
      @SuppressWarnings("unchecked")
      Set<Map.Entry<String, Object>> entrySet = model.entrySet();
      for (Map.Entry<String, Object> entry : entrySet)
      {
        context.put(entry.getKey(), entry.getValue());
      }
    }
    return context;
  }

  private ToolContext getToolContext() throws IllegalStateException, IOException
  {
    if (toolContext == null)
    {
      XmlFactoryConfiguration factoryConfiguration = new XmlFactoryConfiguration("Default Tools");
      factoryConfiguration.read(getServletContext()
          .getResourceAsStream(getToolboxConfigLocation()));
      ToolManager toolManager = new ToolManager();
      toolManager.configure(factoryConfiguration);
      toolContext = toolManager.createContext();
    }
    return toolContext;
  }

I'm using a modified VelocityToolboxView (found somwhere here on stackoverflow) to make use of the Velocity-Tools 2.0 in spring 3.
It looks like it is configuring well, but when I call the $link tool in a .vm file I get an NPE. Scanning through the Velocity-Tools sources I found that it tries to configure the tool with request and response from the ValueParser props, but they are null here.

Here the stack:

LinkTool.configure(ValueParser) line: 100   
LinkTool(SafeConfig).configure(Map) line: 113   
NativeMethodAccessorImpl.invoke0(Method, Object, Object[]) line: not available [native method]  
NativeMethodAccessorImpl.invoke(Object, Object[]) line: 39  
DelegatingMethodAccessorImpl.invoke(Object, Object[]) line: 25  
Method.invoke(Object, Object...) line: 597  
ToolInfo.invoke(Method, Object, Object) line: 363   
ToolInfo.configure(Object, Map<String,Object>) line: 294    
ToolInfo.create(Map<String,Object>) line: 255   
Toolbox.getFromInfo(String, String, Map<String,Object>) line: 152   
Toolbox.get(String, String, Map<String,Object>) line: 112   
ToolContext.findTool(String) line: 221  
ToolContext.get(String) line: 206   
VelocityContext(AbstractContext).get(String) line: 197  

When the ValueParser needs to have the request/response values in its map at this time, where is this normally injected and by whom?

Here is the view class I use:

public class VelocityToolsView extends VelocityToolboxView
{

  private static ToolContext toolContext;

  @Override
  protected Context createVelocityContext(@SuppressWarnings("rawtypes") Map model,
      HttpServletRequest request, HttpServletResponse response) throws IOException
  {
    VelocityContext context = new VelocityContext(getToolContext());
    if (model != null)
    {
      @SuppressWarnings("unchecked")
      Set<Map.Entry<String, Object>> entrySet = model.entrySet();
      for (Map.Entry<String, Object> entry : entrySet)
      {
        context.put(entry.getKey(), entry.getValue());
      }
    }
    return context;
  }

  private ToolContext getToolContext() throws IllegalStateException, IOException
  {
    if (toolContext == null)
    {
      XmlFactoryConfiguration factoryConfiguration = new XmlFactoryConfiguration("Default Tools");
      factoryConfiguration.read(getServletContext()
          .getResourceAsStream(getToolboxConfigLocation()));
      ToolManager toolManager = new ToolManager();
      toolManager.configure(factoryConfiguration);
      toolContext = toolManager.createContext();
    }
    return toolContext;
  }

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

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

发布评论

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

评论(2

蓝眸 2024-12-11 19:18:18

VelocityToolbox 是一个古老的东西,5 年前就被弃用了。我正在使用这种技术将工具集成到 Spring 中。

VelocityToolbox is something from ancient times that was deprecated 5 years ago. I am using this technique to integrate Tools into Spring.

瞎闹 2024-12-11 19:18:18

我也发现了这个问题,并最终使用@serg技术的这个变体解决了它。

I was finding this problem too and eventually solved it using this variation on @serg's technique.

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