1.在pom.xml中添加依赖 

 

<dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity</artifactId>
            <version>1.7</version>
        </dependency>
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity-tools</artifactId>
            <version>2.0</version>
        </dependency>

 


2.从velocity-tools-2.0.jar的org.apache.velocity.tools.view包中复制出velocity.properties和tools.xml文件

到WEB-INF目录下,修改velocity.properties:

[plain] ​​​

 

  1. input.encoding=UTF-8
  2. output.encoding=UTF-8

 


3.配置web.xml文件中的servlet 

[html] ​

 

  1. <servlet>
  2. <servlet-name>velocity</servlet-name>
  3. <servlet-class>org.apache.velocity.tools.view.servlet.VelocityViewServlet</servlet-class>
  4. <init-param>
  5. <param-name>org.apache.velocity.properties</param-name>
  6. <param-value>/WEB-INF/velocity.properties</param-value>
  7. </init-param>
  8. <init-param>
  9. <param-name>org.apache.velocity.toolbox</param-name>
  10. <param-value>/WEB-INF/tools.xml</param-value>
  11. </init-param>
  12. </servlet>
  13. <servlet-mapping>
  14. <servlet-name>velocity</servlet-name>
  15. <url-pattern>*.vm</url-pattern>
  16. </servlet-mapping>