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

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;
       }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值