spring3.0的org.springframework.web.servlet.view.velocity.VelocityToolboxView不支持velocity tools2.0版本,需重写createVelocityContext方法才能支持tools2.0。相关代码如下:
//方法1
<bean id="viewResolver" class="com.sv.web.VelocityLayoutViewResolv erExpand">
<property
name="suffix" value=".html">
</property>
<property
name="toolboxConfigLocation" value="/WEB-INF/velocity-config/tools.xml">
</property>//使用toolboxConfigLocation变量必须将配置文件置于WEB-INF目录或其子目录中
<property
name="viewClass" value="com.sv.web.VelocityLayoutViewExpand "></property>
<property
name="contentType" value="text/html;charset=UTF-8"></property>
</bean>
VelocityLayoutViewExpand 重写方法createVelocityContext:
@Override
protected
Context createVelocityContext(Map<String, Object> model, HttpServletRequest request,
HttpServletResponse
response) throws Exception {
ViewToolContext
ctx;
ctx
= new ViewToolContext(getVelocityEngine(), request, response, getServletContext());
ctx.putAll(model);
if
(this.getToolboxConfigLocation ()
!= null) {
ToolManager
tm = new ToolManager();
tm.setVelocityEngine(getVelocityEngine());
tm.configure(getServletContext().getRealPath(getToolboxConfigLocation ()));
if
(tm.getToolboxFactory().hasTools(Scope.REQUEST)) {
ctx.addToolbox(tm.getToolboxFactory().createToolbox(Scope.REQUEST));
}
if
(tm.getToolboxFactory().hasTools(Scope.APPLICATION)) {
ctx.addToolbox(tm.getToolboxFactory().createToolbox(Scope.APPLICATION));
}
if
(tm.getToolboxFactory().hasTools(Scope.SESSION)) {
ctx.addToolbox(tm.getToolboxFactory().createToolbox(Scope.SESSION));
}
}
return
ctx;
}
//方法2
在viewClass中扩展属性Resource toolboxConfigResource;识别classpath下的tools配置文件
<bean id="viewResolver" class="com.sv.web.VelocityLayoutViewResolv erExpand">
<property
name="suffix" value=".html">
</property>
<property
name="toolboxConfigResource" value="classpath:/context/velocity/tools.xml">
</property>
<property
name="toolboxConfigLocation" value="/WEB-INF/velocity/tools.xml"></property>
<property
name="viewClass" value="com.sv.web.VelocityLayoutViewExpand "></property>
<property
name="contentType" value="text/html;charset=UTF-8"></property>
</bean>
viewClass重写createVelocityContext方法:
@Override
protected
Context createVelocityContext(Map model,
HttpServletRequest
request, HttpServletResponse response)
throws
IllegalStateException, IOException {
ViewToolContext
context = new ViewToolContext(getVelocityEngine(),
request,
response, getServletContext());
context.putAll(model);
if
(null != getToolboxConfigLocation ()
||
null != getToolboxConfigResource ())
{
XmlFactoryConfiguration
cfg = new XmlFactoryConfiguration();
URL
url;
if
(null != getToolboxConfigLocation ())
{
url
= new ServletContextResource(getServletContext(),
getToolboxConfigLocation ()).getURL();
cfg.read(url);
}
else
if (null != getToolboxConfigResource ())
{
url
= getToolboxConfigResource ().getURL();
cfg.read(url);
}
ToolboxFactory
factory = cfg.createFactory();
context.addToolbox(factory.createToolbox(Scope.APPLICATION));
context.addToolbox(factory.createToolbox(Scope.REQUEST));
context.addToolbox(factory.createToolbox(Scope.SESSION));
}
return
context;
}
viewResolver增加属性Resource toolboxConfigResource;
@Override
protected
AbstractUrlBasedView buildView(String viewName) throws Exception {
VelocityLayoutViewExpand view
= (VelocityLayoutViewExpand )
super.buildView(viewName);
//
Use not-null checks to preserve VelocityLayoutView's defaults.
if
(this.layoutUrl != null) {
view.setLayoutUrl(this.layoutUrl);
}
if
(this.layoutKey != null) {
view.setLayoutKey(this.layoutKey);
}
if
(this.screenContentKey != null) {
view.setScreenContentKey(this.screenContentKey);
}
if(this.toolboxConfigResource
!= null){
view.setToolboxConfigResource (this.toolboxConfigResource);
}
return
view;
}
//方法1
<bean id="viewResolver" class="com.sv.web.VelocityLayoutViewResolv
</bean>
VelocityLayoutViewExpand
//方法2
在viewClass中扩展属性Resource toolboxConfigResource;识别classpath下的tools配置文件
<bean id="viewResolver" class="com.sv.web.VelocityLayoutViewResolv
</bean>
viewClass重写createVelocityContext方法:
viewResolver增加属性Resource toolboxConfigResource;