vm到jsp

http://www.iteye.com/topic/135506  这个讲了下velocity

 

 

 

以下是game工程 vm转换成jsp的代码

 

 

private static VelocityEngine ve = null;

在之前的初始化方法中:
// 初始化并取得Velocity引擎
        ve = new VelocityEngine();

// 设置参数
        ve.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH, TEMPLATE_PATH);

        // 处理中文问题
        ve.setProperty(VelocityEngine.INPUT_ENCODING, "UTF-8");
        ve.setProperty(VelocityEngine.OUTPUT_ENCODING, "UTF-8");

        try {
            ve.init();
        } catch (Exception e) {
            e.printStackTrace();
        }


 public void toJsp(String templateName, List servers, List notices) {

        // 输出
        try {
            BufferedWriter writer = new BufferedWriter( // NL
                    new FileWriter(JSP_PATH + templateName + ".jsp"));

            if (templateName.startsWith(Constants.KX_PAGE_PREFIX)) {
                templateName = templateName.substring(3);
            }
            if(ve.resourceExists(templateName + ".vm")){
	            // 取得velocity的模版
	            Template t = ve.getTemplate(templateName + ".vm");
	
	            // 取得velocity的上下文context
	            VelocityContext context = new VelocityContext();
	
	            // 把数据填入上下文
	            context.put("servers", servers);
	            context.put("notices", notices);
	
	            // 转换输出
	            t.merge(context, writer);
	            writer.flush();
	            writer.close();
            }
        } catch (ResourceNotFoundException e) {
            if (logger.isWarnEnabled()) {
                logger.warn(templateName + ".vm not found.");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值