1.加入jar包 commons-beanutils.jar、commons-collections.jar、velocity-1.7-dep.jar,其他的我也不知道要不要
2. java代码
public static String includePage(VelocityContext context,String fileVM){
String path=WebBeanUtils.class.getResource("/").getPath();
path=path.replace("/WEB-INF/classes/", "");
VelocityEngine ve = new VelocityEngine();
Properties properties = new Properties();
properties.setProperty(ve.FILE_RESOURCE_LOADER_PATH,path);
//初始化项目地址,因为getTemplate默认会找绝对地址即d://tomcat/...所以要更改下地址到WebRoot下。
ve.init(properties);
Template t = ve.getTemplate(fileVM);
StringWriter writer = new StringWriter();
t.merge(context, writer);
return writer.toString();
}
3.调用:
VelocityContext context=new VelocityContext();
context.put("list", list);
includePage(context, "/backadmin/vm/user_manage_list.vm");//传入vm在WebRoot下的相对路径
4.VM
#foreach($user in $list)
${user.userId}
#end
jar:http://download.csdn.net/detail/kcity77/9494213