今天用org.springframework.ui.velocity.VelocityEngineFactoryBean 时,velocityCounter这个变量的时候死活不起作用,折腾了良久也不行,网上也找不到任何资料。以前用原生态不与spring集成时还没有问题呢,没办法,只有规规矩矩的把源码下载下来跟跟看了。
最后却发现是原来是配置文件的原因
一部分部分源码
RuntimeConstants /** Counter reference name in #foreach directives. */ String COUNTER_NAME = "directive.foreach.counter.name"; org.apache.velocity.runtime.directive.Foreach counterName = rsvc.getString(RuntimeConstants.COUNTER_NAME); counterName = rsvc.getString(RuntimeConstants.COUNTER_NAME); hasNextName = rsvc.getString(RuntimeConstants.HAS_NEXT_NAME); counterInitialValue = rsvc.getInt(RuntimeConstants.COUNTER_INITIAL_VALUE); if (!"velocityCount".equals(counterName))
原来这个是变量名可以配置的,而我的配置文件
<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean"> <property name="resourceLoaderPath" value="classpath*:template" /> <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 key="directive.foreach.counter.name">loopCounter</prop> <prop key="directive.foreach.counter.initial.value">1</prop> <prop key="file.resource.loader.cache">true</prop> <prop key="parser.pool.size">50</prop> <prop key="input.encoding">UTF-8</prop> <prop key="output.encoding">UTF-8</prop> </props> </property> </bean>
配置成了loopCounter,所以必须用loopCounter才行。