当前位置: 代码迷 >> Web前端 >> Spring的web一部分――Velocity
  详细解决方案

Spring的web一部分――Velocity

热度:351   发布时间:2012-07-08 17:43:44.0
Spring的web部分――Velocity

使用Velocity模版需要引入velocity-1.x.x.jar和common-collections.jar,如果想要在Velocity中使用spring的dataToolAttribute或者numberToolAttribute,还需要Velocity-tools-generic-1.x.jar

1、在*-servlet.xml中配置相关的bean,如下:

 <bean id="velocityConfigurer" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer"> 

    <property name="resourceLoaderPath"> 

      <value>WEB-INF/velocity/</value> 

    </property> 

    </bean>

?这里配置的是引擎自己,告诉在spring中设置Velocity引擎,并设置在哪里能找到velocity的模版,建议在WEB-INF的某个目录下,防止直接被访问,还可以设置其他的属性,如下,是设置循环是从0开始的,默认的是1开始的

<bean id="velocityConfigurer" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer"> 

    <property name="resourceLoaderPath"> 

      <value>WEB-INF/velocity/</value> 

    </property> 

    <property name="velocityProperties"> 

      <props> 

        <prop key="directive.foreach.counter.name">loopCounter</prop> 

        <prop key="directive.foreach.counter.initial.value">0</prop> 

      </props> 

    </property> 

  </bean>

?2、设置解析velocity的视图

<bean id="viewResolver" class="org.springframework. 

          web.servlet.view.velocity.VelocityViewResolver"> 

    <property name="suffix" value=".vm"/> 

  </bean> 

注意:这里把BeanID 设置为viewResolver 。这一点很重要,因为我们并没有配置DispatcherServlet 检测所有的视图解析器。如果要同时使用多个视图解析器,则你很可能需要将这个ID 改成某个更合适的名字(并且是惟一的),比如velocityViewResolver

?

对于非WEB应用,需要者爱application context中配置声明文件VelocityConfigurationFactoryBean

<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
        <property name="velocityProperties">
            <props>
                <prop key="resource.loader">class</prop>
                <prop key="class.resource.loader.class">
                    org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
                </prop>
                <prop key="velocimacro.library"></prop>
            </props>
        </property>
    </bean>
?

?

?

  相关解决方案