您的位置:首页 > 其它

velocity模板发布

2016-07-05 15:21 309 查看
package com.velocitydemo.velocityhandler;

import java.io.StringWriter;

import java.util.ArrayList;

import java.util.Date;

import java.util.List;

import java.util.Properties;

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.runtime.RuntimeConstants;

import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;

@SuppressWarnings("unused")

public class HelloVelocity {

    public static void main(String[] args) {

         VelocityEngine ve = new VelocityEngine();

         ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");

         ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());

         ve.init();

        

         Template t = ve.getTemplate("hello.vm");

         VelocityContext ctx = new VelocityContext();

        

         ctx.put("name", "velocity");

         ctx.put("date", (new Date()).toString());

        

         List temp = new ArrayList();

         temp.add("1");

         temp.add("2");

         ctx.put("list", temp);

        

         StringWriter sw = new StringWriter();

        

         t.merge(ctx, sw);

        

         System.out.println(sw.toString());

         }
}

hello.vm:

效果图:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  velocity 模板发布