刚进公司,就接触了没使用过的velocity,来个链接学习下:http://www.cnblogs.com/chuanjian/p/5501587.html( Velocity教程)。
目前用法是,新建一个继承自VelocityViewServlet 的类,在web.xml中配置好资源接收的路径,同时加上properties文件。在该类中重写handleRequest()方法。由jsp使用request方法调转改为返回Template对象的一个模板。其中返回页面的值使用Context参数put()方法来传递到页面。页面上写velocity的语句即可。
----------------------------我是分割线-----------------------------2017-6-1补充----------------------
现在不仅仅是单独用velocity了,需要将它整合到springmvc里面了。
在springmvc.xml中配置如下:
<bean id="viewResolver"
class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
<property name="cache" value="true" />
<property name="prefix" value=""/>
<property name="suffix" value=".html" />
<property name="contentType" value="text/html;charset=utf-8" />
<!-- 使用Spring 对 Velocity宏命令的支持, 将这个属性设为true -->
<property name="exposeSpringMacroHelpers" value="true" />
<!--session添加到Velocity视图中-->
<property name="exposeRequestAttributes" value="false" />
<property name="exposeSessionAttributes" value="false" />
</bean>
<bean id="velocityConfig"
class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<property name="resourceLoaderPath" value="/WEB-INF/html/" />
<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="directive.parse.max.depth">10</prop>
</props>
</property>
</bean>
其中是属性resourceLoaderPath的值需要根据具体页面位置来决定。
ps:有任何问题,欢迎联系讨论,共同进步。