需要lib
velocity-1.4.jar,
velocity-tools-1.2.jar
copy to /WEB-INF/lib目录下
修改web.xml

<servlet> 

  <servlet-name>action</servlet-name> 

  <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> 

  <init-param> 

   <param-name>config</param-name> 

   <param-value>/WEB-INF/struts-config.xml</param-value> 

  </init-param> 

 </servlet> 

<servlet> 
    <servlet-name>velocity</servlet-name> 
    <servlet-class>org.apache.velocity.tools.view.servlet.VelocityViewServlet</servlet-class> 
    <init-param> 
      <param-name>org.apache.velocity.properties</param-name> 
      <param-value>/WEB-INF/properties/velocity.properties</param-value> 
    </init-param> 
    <init-param> 
      <param-name>org.apache.velocity.toolbox</param-name> 
      <param-value>/WEB-INF/properties/toolbox.xml</param-value>      
    </init-param> 
 </servlet> 

 <!-- Action Servlet Mapping --> 

 <servlet-mapping> 

  <servlet-name>action</servlet-name> 

  <url-pattern>*.do</url-pattern> 

 </servlet-mapping> 

<servlet-mapping> 
    <servlet-name>velocity</servlet-name> 
    <url-pattern>*.vm</url-pattern> 
 </servlet-mapping>


加了红色部分。
新建velocity属性文件(velocity.properties),内容如下

# the following are specified relative to the # root of the webapp because the # ControllerServlet will setup these # properties with the right real path # prepended # file.resource.loader.path=/vm runtime.log=${bluetop.root}/logs/velocity.log #encodeing input.encoding = GBK output.encoding = GBK

写一test class文件


public class VelocityTest extends BaseAction {

	/* (non-Javadoc)
	 * @see com.bluetop.web.action.BaseAction#executeAction(org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	public String executeAction(ActionForm actionFrom,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		List<String> list = new ArrayList<String>();

        list.add("velocity测试");
        list.add("velocity测试 element 2");
        list.add("velocity测试 element 3");
        list.add("velocity测试 element 4");
        request.setAttribute("list", list);
		return "example";
	}
}



配置struts-config.xml


<action path="/vtest" scope="request" type="com.bluetop.demo.VelocityTest">

      <forward name="example" path="/vm/example.vm"/>

  </action>


新建example.vm


## This is an example velocity template

#set( $this = "Velocity")

$this is great!

#foreach( $name in $list )
    $name is great!
#end

#set( $condition = true)

#if ($condition)
    The condition is true!
#else
    The condition is false!
#end



执行/vtest.do输出内容如下:


Velocity is great! velocity测试 is great! velocity测试 element 2 is great! velocity测试 element 3 is great! velocity测试 element 4 is great! The condition is true!


Velocity工作正常,over




一个典型的velocity.properties


#============================================================
# LITERAL SCRIPT
# for Velocity 1.5
#============================================================
stringliterals.interpolate = false
directive.set.null.allowed = true
directive.foreach.maxloops = -1

#============================================================
# RUNTIME LOG
#============================================================
runtime.log.logsystem.class = org.apache.velocity.tools.generic.log.CommonsLogLogSystem
runtime.log.logsystem.commons.logging.name = org.apache.velocity


#============================================================
# RUNTIME PROPERTIES
#============================================================
parser.pool.size = 20

#============================================================
# TEMPLATE ENCODING
#============================================================
input.encoding = UTF-8
output.encoding = UTF-8


#============================================================
# FOREACH PROPERTIES
#============================================================
directive.foreach.counter.name = velocityCount
directive.foreach.counter.initial.value = 1


#============================================================
# RESOURCE MANAGEMENT
#============================================================
#resource.manager.logwhenfound = true
resource.manager.class = org.apache.velocity.runtime.resource.ResourceManagerImpl
resource.manager.cache.class = org.apache.velocity.runtime.resource.ResourceCacheImpl


#============================================================
# RESOURCE LOADER PROPERTIES
#============================================================
#resource.loader = file
#file.resource.loader.description = Velocity File Resource Loader
#file.resource.loader.class = org.apache.velocity.runtime.resource.loader.FileResourceLoader
#file.resource.loader.path = .

# for Development,
# when release modified => file.resource.loader.cache = true
#file.resource.loader.cache = false
#file.resource.loader.modificationCheckInterval = 2


#============================================================
# INCLUDE PROPERTIES
#============================================================
directive.include.output.errormsg.start = <!-- include error :
directive.include.output.errormsg.end = see error log -->


#============================================================
# PARSE PROPERTIES
#============================================================
directive.parse.max.depth = 10


#============================================================
# VELOCIMACRO PROPERTIES
#============================================================
# for Development
webapp.resource.loader.cache = false
webapp.resource.loader.modificationCheckInterval = 1
velocimacro.library.autoreload = true

velocimacro.library = /WEB-INF/conf/vm/VM_global_library.vm,/anyplace/vmmacro/anyplaceMacro.vm
velocimacro.permissions.allow.inline = true
velocimacro.permissions.allow.inline.to.replace.global = true
velocimacro.permissions.allow.inline.local.scope = false
velocimacro.context.localscope = false


#============================================================
# INTERPOLATION
#============================================================
runtime.interpolate.string.literals = true


#============================================================
# PLUGGABLE INTERROSPECTOR
#============================================================
#runtime.introspector.uberspect = org.apache.velocity.util.introspection.UberspectImpl