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();
        }
    }

你可能感兴趣的:(jsp,velocity)