您的位置:首页 > 编程语言 > Java开发

spring3的velocity-tools-2.0配置问题,修改后支持classpath下的工具配置文件

2014-10-10 11:53 337 查看
spring3.0的org.springframework.web.servlet.view.velocity.VelocityToolboxView不支持velocity tools2.0版本,需重写createVelocityContext方法才能支持tools2.0。相关代码如下:

//方法1

<bean id="viewResolver" class="com.sv.web.VelocityLayoutViewResolverExpand">

<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.VelocityLayoutViewResolverExpand">

<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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: