Velocity-Tools in Spring 3 app - Linktool causes NPE, how to solve?
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;
}
VelocityToolbox is something from ancient times that was deprecated 5 years ago. I am using this technique to integrate Tools into Spring.
I was finding this problem too and eventually solved it using this variation on @serg's technique.
精彩评论