Spring 3应用程序中的Velocity-Tools - Linktool导致NPE,如何解决?
我正在使用修改后的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
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.
我也发现了这个问题,并最终使用@serg技术的这个变体解决了它。
I was finding this problem too and eventually solved it using this variation on @serg's technique.