velocity的简介

Velocity是一个基于java的模板引擎(template engine)。它允许任何人仅仅简单的使用模板语言(template language)来引用由java代码定义的对象。 当Velocity应用于web开发时,界面设计人员可以和java程序开发人员同步开发一个遵循MVC架构的web站点,也就是说,页面设计人员可以只 关注页面的显示效果,而由java程序开发人员关注业务逻辑编码。Velocity将java代码从web页面中分离出来,这样为web站点的长期维护提 供了便利,同时也为我们在JSP和PHP之外又提供了一种可选的方案。 Velocity的能力远不止web站点开发这个领域,例如,它可以从模板(template)产生SQL和PostScript、XML,它也可以被当 作一个独立工具来产生源代码和报告,或者作为其他系统的集成组件使用。Velocity也可以为Turbine web开发架构提供模板服务(template service)。Velocity+Turbine提供一个模板服务的方式允许一个web应用以一个真正的MVC模型进行开发。

 

Java代码   收藏代码
  1. package com.velocity.demo;  
  2.   
  3. import java.io.StringWriter;  
  4. import java.util.ArrayList;  
  5. import java.util.HashMap;  
  6. import java.util.Map;  
  7. import java.util.Properties;  
  8.   
  9. import org.apache.velocity.Template;  
  10. import org.apache.velocity.VelocityContext;  
  11. import org.apache.velocity.app.Velocity;  
  12. import org.apache.velocity.app.VelocityEngine;  
  13. import org.apache.velocity.exception.ParseErrorException;  
  14. import org.apache.velocity.exception.ResourceNotFoundException;  
  15.   
  16. public class PromotedVelocityDemo  
  17. {  
  18.     public static void main(String[] args) throws Exception, ParseErrorException, Exception  
  19.     {  
  20.         VelocityEngine ve = getVelocityEngine();  
  21.         VelocityContext context = getVelocityContext();  
  22.         Template template = ve.getTemplate("com/velocity/demo/promotedTemple");  
  23.         if(template != null){  
  24.             StringWriter getWriter = new StringWriter();  
  25.             template.merge(context, getWriter);  
  26.             System.out.println(getWriter);  
  27.         }  
  28.           
  29.     }   
  30.      public static ArrayList getNames() {    
  31.             ArrayList list = new ArrayList();    
  32.             list.add("刘少奇");    
  33.             list.add("李逵");    
  34.             list.add("王熙凤");    
  35.             list.add("李四光");    
  36.             return list;    
  37.         }    
  38.     public static ArrayList getNamesMap() {     
  39.         ArrayList list = new ArrayList();    
  40.         Map map = new HashMap();        
  41.         map.put("name""书包");    
  42.         map.put("price""$100.00");    
  43.         list.add( map );        
  44.         map = new HashMap();    
  45.         map.put("name""唱片");    
  46.         map.put("price""$59.99");    
  47.         list.add( map );        
  48.         map = new HashMap();    
  49.         map.put("name""小菜");    
  50.         map.put("price""$3.99");    
  51.         list.add( map );    
  52.         return list;    
  53.     }    
  54.     public static Map getUserInfo(){  
  55.         Map<String,String> para = new HashMap<String,String>();  
  56.         para.put("name""dirk.zhang");  
  57.         para.put("age""20");  
  58.         para.put("sex""male");  
  59.         return para;  
  60.     }  
  61.     public static VelocityContext getVelocityContext(){  
  62.         VelocityContext context = new VelocityContext();  
  63.         context.put("list", getNames());  
  64.         context.put("listMap", getNamesMap());  
  65.         Map<String,String> para = getUserInfo();  
  66.         if(para.size() >0)  
  67.             for(String key : para.keySet())  
  68.                 context.put(key, para.get(key));  
  69.         return context;  
  70.     }  
  71.     public static VelocityEngine getVelocityEngine() throws Exception{  
  72.         VelocityEngine ve = new VelocityEngine();  
  73.         Properties properties = new Properties();  
  74.         String fileDir = Thread.currentThread().getContextClassLoader().getResource("").getPath();  
  75. //      ve.setProperty(Velocity.RESOURCE_LOADER, "class");  
  76. //      ve.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");  
  77.         properties.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH,fileDir);  
  78.         properties.setProperty(Velocity.ENCODING_DEFAULT, "utf-8");  
  79.         properties.setProperty(Velocity.INPUT_ENCODING, "utf-8");  
  80.         properties.setProperty(Velocity.OUTPUT_ENCODING, "utf-8");  
  81.         ve.init(properties);  
  82.         return ve;  
  83.           
  84.     }  
  85.   
  86. }  

 下面是模板文件:

Java代码   收藏代码
  1. ##声明一个变量:this并且设置值为:Velocity    
  2. #set( $this = "咫尺天涯")    
  3. ##使用设置的变量值    
  4. 这里是$this的博客!    
  5. ##for打印集合信息数据    
  6. #foreach( $name in $list )    
  7. 姓名:$name!    
  8. #end    
  9. ##声明一个布尔值    
  10. #set( $condition = true)    
  11. ## if else分支判断    
  12. #if ($condition)    
  13. The condition is true!    
  14. #else    
  15. The condition is false!    
  16. #end    
  17. 姓名:$name,年龄:$age,性别:$sex    
  18. #foreach( $p in $listMap )    
  19. 项目:$p.name,价格:$p.price;    
  20. #end    
 velocity使用简单,一看便知,老少皆宜的好东东
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值