Velocity - Snippets

Velocity是一个基于java的模板引擎(template engine)。它允许任何人仅仅简单的使用模板语言(template language)来引用由java代码定义的对象。

Java工程中引入Velocity的包, 这里是velocity-1.4.jar, velocity-dep-1.4.jar.

TestVelocity.java:

package  tony.test;

import  java.io.StringWriter;
import  java.util.ArrayList;
import  java.util.HashMap;
import  java.util.Iterator;
import  java.util.List;
import  java.util.Map;
import  java.util.Properties;

import  org.apache.velocity.VelocityContext;
import  org.apache.velocity.app.Velocity;
import  org.apache.velocity.app.VelocityEngine;
import  org.apache.velocity.context.Context;
import  org.apache.velocity.exception.MethodInvocationException;
import  org.apache.velocity.exception.ParseErrorException;
import  org.apache.velocity.exception.ResourceNotFoundException;
import  org.apache.velocity.runtime.RuntimeConstants;

/**
 * 
@author Tony
 
*/

public   class  TestVelocity
{
    
private static VelocityEngine engine = null;
    
    
/**
     * Initialize velocity
     
*/

    
public static void initVelocity() throws Exception
    
{
        engine 
= new VelocityEngine();
        Properties prop 
= new Properties();
        
// Set the template file path
        prop.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, "E:/templates");
        engine.init(prop);
    }

    
    
/**
     * 
@param templeteFileName: Velocity templete file name.
     * 
@param values: Velocity templete variables and values map.
     * 
@return Generated content string.
     
*/

    
private static String generateContent(String templeteFileName, Map values) throws ResourceNotFoundException, ParseErrorException, MethodInvocationException, Exception
    
{
        Context context 
= new VelocityContext();
        
for (Iterator it=values.entrySet().iterator(); it.hasNext();)
        
{
            Map.Entry entry 
= (Map.Entry)it.next();
            context.put((String)entry.getKey(), entry.getValue());
        }

        StringWriter wr 
= new StringWriter();
        engine.mergeTemplate(templeteFileName, RuntimeConstants.ENCODING_DEFAULT, context, wr);
        
        
return wr.toString();
    }

    
    
public static void main(String[] args) throws Exception
    
{
        initVelocity();
        
        List dothings 
= new ArrayList();
        dothings.add(
new DoThings("Haha""Giggle"));
        dothings.add(
new DoThings("Hehe""Smile"));
        dothings.add(
new DoThings("Kaka""Die"));
        
        Map values 
= new HashMap();
        values.put(
"YOUR_CHARACTERISTIC","old");
        values.put(
"YOUR_NAME","Tony");
        values.put(
"DO_THINGS""laugh");
        values.put(
"REPEAT_TIME"new Integer(dothings.size()));
        values.put(
"ALL_TIMES", dothings);
        
        System.out.println(generateContent(
"velocity.helloworld.vm", values));
    }

}

DoThings.java: 

package  tony.test;

/**
 * 
@author Tony
 * The class should be public, and implement get/set.
 
*/

public   class  DoThings
{
    
private String name;
    
private String things;

    
public DoThings(String name, String things)
    
{
        
this.name = name;
        
this.things = things;
    }


    
public String getName()
    
{
        
return name;
    }


    
public void setName(String name)
    
{
        
this.name = name;
    }


    
public String getThings()
    
{
        
return things;
    }


    
public void setThings(String things)
    
{
        
this.things = things;
    }

}

 E:/templates/velocity.helloworld.vm(完整路径):

< html >
< body >
I am the $YOUR_CHARACTERISTIC $YOUR_NAME. 
< br  />< br  />
I will $DO_THINGS for $REPEAT_TIME times:
< br  />< br  />
#foreach($EACH_TIME in $ALL_TIMES)
$velocityCount. $EACH_TIME.name -- $EACH_TIME.things
< br  />
#end
< br  />
#if($YOUR_NAME == "Tony")
Best wishes!
#else
Go to die!
#end
</ body >
</ html >

 生成结果:

< html >
< body >
I am the old Tony. 
< br  />< br  />
I will laugh for 3 times:
< br  />< br  />
1. Haha -- Giggle
< br  />
2. Hehe -- Smile
< br  />
3. Kaka -- Die
< br  />
< br  />
Best wishes!
</ body >
</ html >

 

 

一份不错的参考:

Velocity用户手册---中文版(http://www.ijsp.net/2/2003-9/27/0000434.shtml).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值