petstoreemail.java

来自「Turbine示例」· Java 代码 · 共 75 行

JAVA
75
字号
import java.io.StringWriter;import java.util.List;import java.util.ArrayList;import java.util.Map;import java.util.HashMap;import org.apache.velocity.Template;import org.apache.velocity.VelocityContext;import org.apache.velocity.app.VelocityEngine;public class PetStoreEmail{    public static void main( String[] args )        throws Exception    {        /*         *   first, get and initialize an engine         */        VelocityEngine ve = new VelocityEngine();        ve.init();        /*         *   organize our data          */        ArrayList list = new ArrayList();        Map map = new HashMap();        map.put("name", "horse");        map.put("price", "$100.00");        list.add( map );         map = new HashMap();        map.put("name", "dog");        map.put("price", "$59.99");        list.add( map );        map = new HashMap();        map.put("name", "bear");        map.put("price", "$3.99");        list.add( map );        /*         *  add that list to a VelocityContext         */        VelocityContext context = new VelocityContext();        context.put("petList", list);        /*         *   get the Template           */        Template t = ve.getTemplate( "petstoreemail.vm" );        /*         *  now render the template into a Writer, here          *  a StringWriter          */        StringWriter writer = new StringWriter();        t.merge( context, writer );        /*         *  use the output in the body of your emails         */        System.out.println( writer.toString() );    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?