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));
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值