velocity 自定义工具类 ,转自:http://yonge812.iteye.com/blog/1535290,有删减
依赖<!-- velocity模板引擎 -->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.7</version>
</dependency>
<!-- velocity工具 -->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-tools</artifactId>
<version>2.0</version>
</dependency>
1.定义一个工具类
package com.openfire.plugin.usionmrg.web.tools;
/**
* velocity工具类
*
* @author 20005
* @createDate 2014-7-24 上午09:26:18
*/
public class VelocityTool {
public String out() {
return "velocityTool";
}
}
2.编写velocityToolbox.xml velocity 工具类配置文件
<?xml version="1.0" encoding="UTF-8"?>
<toolbox>
<tool>
<!-- 模板中调用名 -->
<key>mysys</key>
<scope>application</scope>
<!-- 类全名 -->
<class>com.aomaisoft.openfire.plugin.usionmrg.web.tools.VelocityTool</class>
</tool>
</toolbox>
3.在springMVC配置文件中定义视图解析器时加入参数toolboxConfigLocation
<!-- 视图解析 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
<property name="prefix" value="/" />
<property name="suffix" value=".html" />
<property name="contentType" value="text/html;charset=utf-8"></property>
<!-- velocity工具类配置 -->
<property name="toolboxConfigLocation" value="/WEB-INF/velocityToolbox.xml" />
</bean>