关于velocity tool2.0+springMVC的配置


1、  layout 的配置

在spring的配置文件中,先配置对于vm文件的解析

如下

</pre><p></p><p align="left"><pre name="code" class="html"><bean id="viewResolver"class="org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver"> 
<!--    view解析器需要使用需要使用org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver
     当不需要使用layout布局时,使用org.springframework.web.servlet.view.velocity.VelocityViewResolver
-->
    <property name="viewClass" value="com.cy.wxs.velocity.VelocityToolbox2View"/>
       <!-- <property name="viewClass"value="org.springframework.web.servlet.view.velocity.VelocityLayoutView"/>
 -->
   <property name="cache" value="true"/>
    <property name="prefix" value=""/>
    <property name="suffix" value=".vm"/>
   <property name="contentType" value="text/html;charset=utf-8" />
    <property name="layoutUrl" value="/layout/layout.vm"/>
    <property name="requestContextAttribute" value="ac" />
    <property name="order" value="1"/>
    <property name="numberToolAttribute" value="numbertool" />
    <property name="dateToolAttribute" value="datetool" />
     <property name="toolboxConfigLocation"value="WEB-INF/vm/conf/velocity-toolbox.xml"/>             
       
</bean>
然后添加velocityConfig配置,如下:
<bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
   <property name="resourceLoaderPath" value="/WEB-INF/vm/"/>
 <!--    <propertyname="configLocation" value="/WEB-INF/vm/conf/velocity.properties"/>-->
 <property name="velocityProperties">
          <props>
            <prop key="input.encoding">UTF-8</prop>
            <prop key="output.encoding">UTF-8</prop>
            <!-- <prop key="contentType">text/html;charset=UTF-8</prop>-->
            <prop key="velocimacro.library">conf/VM_global_library.vm,conf/VM_wml_library.vm,conf/VM_wxs_library.vm</prop>
          </props>
        </property>
</bean>


 

 

如上:layoutUrl配置默认模板的路径,路径= resourceLoaderPath+ layoutUrl即可以得到布局文件的绝对路径

如此layout即配置成功,如果在实际的工程中,需要切换到其他的布局文件,即可在对应的vm文件中的开始添加#set($layout="layout/layout.vm"),配置文件的实际路径,为 resourceLoaderPath加layout。

 

2、添加宏文件的问题,可以在velocityConfig中添加  <prop key="velocimacro.library">conf/VM_global_library.vm,conf/VM_wml_library.vm,conf/VM_wxs_library.vm</prop>

value为字符串数组,用(,)隔开即可配置多个宏文件)

3、配置velocity-tool

在实际使用中,需要配置一些velocity-tool

spring对最新的velocity tool2.0 的支持不是太好,所以需要扩展viewClass类,才能读取velocitytool.Xml文件。

在没有使用layoutvelocity中,需要可以直接继承VelocityToolboxView类,然后重写createVelocityContext方法,但是VelocityLayoutViewResolver中却无法加载继承VelocityToolboxView的类,而是需要继承VelocityLayoutView,去查找源文件的类,发现VelocityLayoutView继承一样VelocityToolboxView类,所以直接继承VelocityLayoutView然后重写createVelocityContext方法,如下:

package com.cy.wxs.velocity;
 
import java.util.Map;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.apache.velocity.context.Context;
import org.apache.velocity.tools.Scope;
import org.apache.velocity.tools.ToolManager;
import org.apache.velocity.tools.view.ViewToolContext;
import org.springframework.web.servlet.view.velocity.VelocityLayoutView;
 
 
public classVelocityToolbox2View extends VelocityLayoutView{
     @Override
        protected ContextcreateVelocityContext(Map<String, Object> model,
                HttpServletRequest request,HttpServletResponse response)
                throws Exception {// Create a
                                    // ChainedContext
                                    // instance.
            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;
        }
 
}


4、关于velocity tool2.0

Velocity的配置文件较与之前的配置的版本,在配置上有一些改变,具体的配置方法在velocity-tool2.0.jar包的位置:org.apache.velocity.tools.generic

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值