[英]Velocity + Spring
我正在尝试使用上述组件设置一个Webapp。 除了最后一个障碍,我已经跳过了所有集成Spring&Velocity Tools的障碍。 我今天早上看到了这篇文章 ,并用与提供的答案稍有不同的答案对其进行了更新。 但是,一旦我尝试将ParameterTool添加到我的模板之一中,如下所示:
#foreach( $key in $params.keySet() )
$key = $params.getValue($key)
<br />
#end
我收到NPE java.lang.UnsupportedOperationException:请求为空。 必须先初始化ParameterTool! 根据我所读的内容,这意味着该工具已正确配置,只是无法访问该请求。 注意:我也收到接受的解决方案的错误。
有没有人能够在Spring上成功使用这些工具? 似乎是已知的缺陷,因为此Open Jira SPR-5514有一个Open Jira
对此问题的“接受的答案 ”进行了稍微修改的版本可以解决此问题。
除了返回ViewContext,您还需要返回ViewToolContext。 您还需要准备工具箱,并根据需要在会话/请求中进行设置:
您将需要以所需的任何方式来初始化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
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.