欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

Velocity 使用string作为模板内容,velocitystring,Velocity 使用s

来源: javaer 分享于  点击 14727 次 点评:186

Velocity 使用string作为模板内容,velocitystring,Velocity 使用s


Velocity 使用string作为模板内容

    public String execute(String templateText, Map<String, Object> params) {        Template template;        if (vmTemplateCache.containsKey(templateText)) {            template = vmTemplateCache.get(templateText);        } else {            RuntimeServices runtimeServices = RuntimeSingleton.getRuntimeServices();            StringReader reader = new StringReader(templateText);            SimpleNode node = null;            try {                node = runtimeServices.parse(reader, templateText);            } catch (ParseException e) {                throw new ConfigurationException("template config error");            }            template = new Template();            template.setRuntimeServices(runtimeServices);            template.setData(node);            template.initDocument();            vmTemplateCache.put(templateText, template);        }        Context vc = new VelocityContext();        vc.put("dateTool", new DateTool());        vc.put("numberTool", new NumberTool());        vc.put("escapeTool", new EscapeTool());        vc.put("String", String.class);        vc.put("GU", GenUtils.class);        // 把map中的变量加入        for (String key : params.keySet()) {            vc.put(key, params.get(key));        }        StringWriter writer = new StringWriter();        template.merge(vc, writer);        return writer.toString();    }
相关栏目:

用户点评