1. 首先通过pom.xml自动加载velocity扩展包到工程:
1 <dependency> 2 <groupId>velocity</groupId> 3 <artifactId>velocity</artifactId> 4 <version>1.5</version> 5 </dependency>
2. 然后在自动装载bean的xml里边添加如下配置:
1 <bean id="velocityConfig" 2 class="org.springframework.web.servlet.view.velocity.VelocityConfigurer"> 3 <property name="resourceLoaderPath" value="/WEB-INF/views/" /> 4 <property name="velocityPropertiesMap"> 5 <props> 6 <prop key="input.encoding">utf-8</prop> 7 <prop key="output.encoding">utf-8</prop> 8 </props> 9 </property> 10 </bean> 11 <bean id="viewResolver" 12 class="org.springframework.web.servlet.view.velocity.VelocityViewResolver"> 13 <property name="cache" value="true" /> 14 <property name="exposeSpringMacroHelpers" value="true" /> 15 <property name="suffix" value=".vm" /> 16 <property name="contentType" value="text/html;charset=UTF-8" /> 17 </bean>
通过如上配置,Spring工程就支持Velocity模版引擎了,模版文件位于:/WEB-INF/views/目录下,指定输入和输出编码为UTF-8编码,同时指定了模版的后缀名称为.vm结尾。