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;
}
//方法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;
}
相关文章推荐
- 加载spring配置文件的工具类
- Dwr2+Log4j+Struts1.2+Spring2.0+Hibernate3.1_in_MyEclipse5.5配置问题及解决方法
- 基于Spring可扩展Schema提供自定义配置支持(spring配置文件中 配置标签支持)
- 用ClassPathXmlApplicationContext读取Spring配置文件的路径问题
- [我的Linux技术支持生涯] bash配置文件在登陆时不起作用问题
- Spring2.0配置文件加载util标签的Schema
- spring工具-- 配置文件初始化.
- Spring中使用classpath加载配置文件浅析 1
- 《Spring 2.0 技术手册》读书笔记四-非XML的Bean定义文件配置
- struts1和spring进行整合的时候struts-config.xml文件的配置顺序问题
- Spring的bean 配置文件路径问题(转载)
- Spring配置文件中SessionFactory引用Hibernate配置产生问题的思考
- Spring中使用classpath加载配置文件浅析
- spring多个配置文件的问题。。
- Spring中使用classpath加载配置文件浅析 2
- xFire在使用Spring2.5,设定文件配置记述中需要注意的问题
- Spring中使用classpath加载配置文件浅析
- spring 配置文件中datasource 从properties文件中获取mysql数据库连接参数遇到的问题
- struts2.0中spring配置文件中action的属性 scope="prototype"
- Spring中使用classpath加载配置文件浅析