VelocityUtils

/** * copyright (c) 2005-2010 springside.org.cn * * licensed under the apache license, version 2.0 (the "license"); *  * $id: velocityutils.java 1211 2010-09-10 16:20:45z calvinxiu $ */package com.juqi.group.common.util;import java.io.ioexception;import java.io.stringwriter;import java.util.arraylist;import java.util.hashmap;import java.util.list;import java.util.map;import org.apache.velocity.template;import org.apache.velocity.velocitycontext;import org.apache.velocity.app.velocity;import org.apache.velocity.app.velocityengine;import org.apache.velocity.exception.parseerrorexception;import org.apache.velocity.exception.resourcenotfoundexception;import com.juqi.group.service.serviceexception;/** * 使用velocity生成内容的工具类. *  */public class velocityutils {	static {		try {			velocity.setproperty("input.encoding", "utf-8");			velocity.setproperty("output.encoding", "utf-8");			velocity.setproperty("directive.foreach.counter.name", "c");			velocity.init();		} catch (exception e) {			throw new runtimeexception(					"exception occurs while initialize the velociy.", e);		}	}	/**	 * 渲染内容.	 * 	 * @param template	 *            模板内容.	 * @param model	 *            变量map.	 * @throws ioexception 	 */	public static string render(string template, map<string, ?> model) throws serviceexception {		try {			velocitycontext velocitycontext = new velocitycontext(model);			stringwriter result = new stringwriter();			velocity.evaluate(velocitycontext, result, "", template);			return result.tostring();		}		catch (exception e) {			throw new serviceexception("解析模板异常", e);					}	}	private static velocityengine velocityengine() {		velocityengine ve = new velocityengine();		// 写绝对路径		ve.setproperty(velocityengine.file_resource_loader_path,				"src/main/resources");		ve.setproperty("input.encoding", "utf-8");		ve.setproperty("output.encoding", "utf-8");		ve.setproperty("directive.foreach.counter.name", "c");		try {			ve.init();		} catch (exception e) {			e.printstacktrace();		}		return ve;	}	public static string parse(map<string, object> map) {		velocitycontext context = new velocitycontext(map);		try {			template template = velocityengine().gettemplate("index.vm");			stringwriter sw = new stringwriter();			if (template != null)				template.merge(context, sw);			sw.flush();			sw.close();			return sw.tostring();		} catch (resourcenotfoundexception e) {			e.printstacktrace();		} catch (parseerrorexception e) {			e.printstacktrace();		} catch (exception e) {			e.printstacktrace();		}		return null;	}	public static void main(string[] args) {		list<string> list = new arraylist<string>();		list.add("1");		list.add("2");		list.add("3");		list.add("4");		list.add("5");		map<string, object> m = new hashmap<string, object>();		m.put("list", list);		system.out.println(parse(m));	}}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值