velocity模板引擎学习(4)-在standalone的java application中使用velocity及velocity-tools

通常velocity是配合spring mvc之类的框架在web中使用,但velocity本身其实对运行环境没有过多的限制,在单独的java application中也可以独立使用,下面演示了利用velocity模板引擎生成 『每日发货单』邮件内容:

一、先定义邮件内容模板:mail-template.vm

<string>用户,您好:</string>
<h2>以下是 $date.format('yyyy-MM-dd',$model.deliverDate,$convert.toLocale("en_US")) 的发货清单,请核对!</h2>
<ul>
    #foreach ($item in $model.items)
        <li>$velocityCount . $item.productName / $item.productNum 件</li>
    #end
</ul>

这里面涉及到二个java类:Order(发货订单)及OrderItem(订单明细)

import java.util.Date;
import java.util.List;


public class Order {

    private Date deliverDate;

    private List<OrderItem> items;

    public Date getDeliverDate() {
        return deliverDate;
    }

    public void setDeliverDate(Date deliverDate) {
        this.deliverDate = deliverDate;
    }

    public List<OrderItem> getItems() {
        return items;
    }

    public void setItems(List<OrderItem> items) {
        this.items = items;
    }
}

public class OrderItem {

    private String productName;
    private int productNum;

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public int getProductNum() {
        return productNum;
    }

    public void setProductNum(int productNum) {
        this.productNum = productNum;
    }
}

 

二、使用示例

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
import org.apache.velocity.tools.generic.ConversionTool;
import org.apache.velocity.tools.generic.DateTool;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Date;


public class VelocityTest {

    public static void main(String[] args) {        

        VelocityEngine ve = new VelocityEngine();
        //定义vm资源加载的路径为classpath所在目录
        ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
        ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());

        ve.init();

        //加载模板
        Template t = ve.getTemplate("mail-template.vm", "utf-8");
        VelocityContext ctx = new VelocityContext();

        //加载velocity-tools
        ctx.put("date", new DateTool());
        ctx.put("convert", new ConversionTool());

        //放入要绑定的对象
        ctx.put("model", getOrder());

        StringWriter sw = new StringWriter();

        t.merge(ctx, sw);

        //输出
        System.out.println(sw.toString());
    }

    private static Order getOrder() {

        Order o = new Order();
        o.setDeliverDate(new Date());
        o.setItems(new ArrayList<OrderItem>());

        OrderItem item1 = new OrderItem();
        item1.setProductName("iphone");
        item1.setProductNum(1);

        OrderItem item2 = new OrderItem();
        item2.setProductName("mac pro");
        item2.setProductNum(2);

        o.getItems().add(item1);
        o.getItems().add(item2);

        return o;
    }
}

运行的输出结果:

<string>用户 您好:</string>
<h2>以下是 2015-09-12 的发货清单,请核对!</h2>
<ul>
            <li>1 . iphone / 1 件</li>
            <li>2 . mac pro / 2 件</li>
    </ul>

 

参考文章:

http://www.ibm.com/developerworks/cn/java/j-lo-velocity1/index.html

 

上一篇:纯css选取指定元素(兼容IE6/7/8)


下一篇:EditText初始不获得焦点及输入框被遮挡问题