Velocity&Freemarker 的比较

模板技术在现代的软件开发中有着重要的地位,而目前最流行的两种模板技术恐怕要算freemarker和velocity了,webwork2.2对两者都有不错的支持,也就是说在webwork2中你可以随意选择使用freemarker或velocity作为view,模板技术作为view的好处是很多,尤其和jsp比较起来优点更大,众所周知jsp需要在第一次被执行的时候编译成servlet,那么这个过程是很慢的,当然很多应用服务器都提供预编译的功能,但是在开发的时候仍然给我们程序员带来了很多痛苦,每次修改都要多几秒钟,那在一天的开发中就有很多时间浪费在jsp的编译上了。用webwork in action的作者的话来说:“每次修改之后重新运行都要等等几秒是令人失望的,而频繁地修改jsp更是会令你的失望情绪变本加厉“。我们把模板技术引入到view中去可以带来更好的开发效率,而且模板的速度要比jsp快(虽然编译过后的jsp在速度上已经满足我的需求了,呵呵)。 当然模板技术可以用在很多领域,可不只在view那里。我们可以通过模板技术来生成xml,生成jsp,生成java文件等等,说到这里,大家通常会使用模板技术用在公司的框架里,这样就可以很快速的生成添删改查的代码,需要的只是模板,其他比如还有邮件模板等等。 

以上是模板的作用,当然模板还有其他领域的应用,希望能和大家多讨论,提高我们的生产效率。 

那么现在开源的模板技术有好几种,多了之后就有一个选择的问题了,如何选择一个满足自己需要的模板的呢,我大概了看了一下两种模板技术,写了一个例子,我使用了几种设计模式来完成了这个例子,这个例子中,我同时使用了freemarker和velocity,这样同学们可以通过代码很直观的比较两种模板技术,通过这个例子,我认识到freemarker在功能上要比velocity强大 
1在view层的时候,它提供了format日期和数字的功能,我想大家都有在页面上format日期或数字的经验,用jsp的同学可能对jstl的fmt标签很有感情,使用了freemarker之后也可以使用freemarker提供的功能来formmat日期和数据,这个功能我想是很贴心的 

2通过我的使用我发现freemaker的eclipseplugin要比velocity的eclipseplugin好,如果你是用idea那很遗憾,我没有找到类似的插件。好在很多地方呢,我看到的是freemarker的插件除了支持freemarker语法也支持html语句,而velocity的插件貌似只支持velocity的语法,html就只是用普通的文本来显示了,在这一点上freemarker占上风了(不要和我说高手都是用windows记事本之类的话,这本来就违背了模板技术的初衷) 

3freemarker对jsptag的支持很好,算了,不到迫不得已还是不要这样做吧。 

还有就是两者的语法格式,这一点上不同的人有不同倾向 

下面就介绍一下这个例子吧 

Java代码   收藏代码
  1.  
  2. public class TemplateTest {  
  3.   
  4.     /** 
  5.      * @param args 
  6.      */  
  7.     public static void main(String[] args) throws Exception{  
  8.         /* 准备数据 */  
  9.         Map latest = new HashMap();  
  10.         latest.put("url""products/greenmouse.html");  
  11.         latest.put("name""green mouse");  
  12.           
  13.         Map root = new HashMap();  
  14.         root.put("user""Big Joe");  
  15.         root.put("latestProduct", latest);  
  16.         root.put("number"new Long(2222));  
  17.         root.put("date",new Date());  
  18.           
  19.         List listTest = new ArrayList();  
  20.         listTest.add("1");  
  21.         listTest.add("2");  
  22.           
  23.         root.put("list",listTest);  
  24.           
  25.         TemplateEngine freemarkerEngine = (TemplateEngine)TemplateFactory.getInstance().getBean("freemarker");  
  26.         freemarkerEngine.run(root);//使用freemarker模板技术  
  27.           
  28.         TemplateEngine velocityEngine = (TemplateEngine)TemplateFactory.getInstance().getBean("velocity");  
  29.         velocityEngine.run(root);//使用velocity模板技术  
  30.     }  
  31.   
  32. }  


工厂类,用来得到模板引擎 
Java代码   收藏代码

  1. public class TemplateFactory {  
  2.     private static TemplateFactory instance;  
  3.     private Map objectMap;  
  4.       
  5.     static{  
  6.         instance = new TemplateFactory();  
  7.     }  
  8.       
  9.     public TemplateFactory() {  
  10.         super();  
  11.         this.objectMap = new HashMap();  
  12.         synchronized (this) {  
  13.             objectMap.put("freemarker"new FreemarkerTemplateEngine(){  
  14.                 public String getTemplatePath() {  
  15.                     return "template";  
  16.                 }  
  17.             });  
  18.               
  19.             objectMap.put("velocity"new VelocityTemplateEngine());  
  20.         }  
  21.     }  
  22.   
  23.     public static TemplateFactory getInstance(){  
  24.         return instance;  
  25.     }  
  26.       
  27.     /** 
  28.      * 模仿spring的工厂 
  29.      * @param beanName 
  30.      * @return 
  31.      */  
  32.     public Object getBean(String beanName){  
  33.         return objectMap.get(beanName);  
  34.     }  
  35.   
  36. }  
引擎接口 
Java代码   收藏代码

  1. public interface TemplateEngine {  
  2.       
  3.     void run(Map context)throws Exception;  
  4.   
  5. }  
模板引擎的实现使用method template模式,因为有两个实现,这两个实现又存在公共的逻辑,所以选择了这个模式 

Java代码   收藏代码

  1. public abstract class AbstractTemplateEngine implements TemplateEngine{  
  2.   
  3.     public abstract String getTemplatePath();  
  4.       
  5.     public abstract String getTemplate();  
  6.       
  7.     public abstract String getEngineType();  
  8.       
  9.     public void run(Map context)throws Exception{  
  10.         if(Constants.ENGINE_TYPE_FREEMARKER.equals(getEngineType()))  
  11.             executeFreemarker(context);  
  12.         else  
  13.             executeVelocity(context);  
  14.     }  
  15.       
  16.     private void executeFreemarker(Map context)throws Exception{  
  17.         Configuration cfg = new Configuration();  
  18.         cfg.setDirectoryForTemplateLoading(  
  19.                 new File(getTemplatePath()));  
  20.         cfg.setObjectWrapper(new DefaultObjectWrapper());  
  21.           
  22.         cfg.setCacheStorage(new freemarker.cache.MruCacheStorage(20250));  
  23.                   
  24.         Template temp = cfg.getTemplate(getTemplate());  
  25.   
  26.         Writer out = new OutputStreamWriter(System.out);  
  27.         temp.process(context, out);  
  28.         out.flush();  
  29.     }  
  30.       
  31.     private void executeVelocity(Map root)throws Exception{  
  32.           
  33.         Velocity.init();  
  34.         VelocityContext context = new VelocityContext(root);  
  35.         org.apache.velocity.Template template = null;  
  36.           
  37.         template = Velocity.getTemplate(getTemplatePath()+getTemplate());  
  38.           
  39.         StringWriter sw = new StringWriter();  
  40.         template.merge( context, sw );  
  41.         System.out.print(sw.toString());  
  42.   
  43.     }  
  44.   
  45. }  



这个是freemarker实现 
Java代码   收藏代码

  1. public class FreemarkerTemplateEngine extends AbstractTemplateEngine{  
  2.     private static final String DEFAULT_TEMPLATE = "FreemarkerExample.ftl";  
  3.       
  4.     /** 
  5.      * 这个方法应该实现的是读取配置文件 
  6.      */  
  7.     public String getTemplatePath() {  
  8.         return null;  
  9.     }  
  10.       
  11.     public void run(Map root) throws Exception{  
  12.         super.run(root);  
  13.     }  
  14.   
  15.     public String getTemplate() {  
  16.         // TODO Auto-generated method stub  
  17.         return DEFAULT_TEMPLATE;  
  18.     }  
  19.   
  20.     public String getEngineType() {  
  21.         return Constants.ENGINE_TYPE_FREEMARKER;  
  22.     }  
  23. }  
这个是velocity实现 
Java代码   收藏代码
  1.  
  2. public class VelocityTemplateEngine extends AbstractTemplateEngine{  
  3.   
  4. private static final String DEFAULT_TEMPLATE = "VelocityExample.vm";  
  5.   
  6.     public String getTemplatePath() {  
  7.         return "/template/";  
  8.     }  
  9.       
  10.     public void run(Map map) throws Exception{  
  11.         super.run(map);  
  12.     }  
  13.   
  14.     public String getTemplate() {  
  15.         // TODO Auto-generated method stub  
  16.         return DEFAULT_TEMPLATE;  
  17.     }  
  18.   
  19.     public String getEngineType() {  
  20.         // TODO Auto-generated method stub  
  21.         return Constants.ENGINE_TYPE_VELOCITY;  
  22.     }  
  23. }  


以下是模板 
1,freemarker模板 
Java代码   收藏代码
  1. freemarker template test:  
  2. string test-----------${user}-----------${number}-----------${latestProduct.url}-----------${latestProduct.name}  
  3. condition test-----------  
  4. <#if user == "Big Joe">  
  5. list iterator-----------  
  6. <#list list as aa>  
  7. ${aa}  
  8. </#list>   
  9. </#if>  
  10. date test-----------${date?string("MMM/dd/yyyy")}  


2,velocity模板 

Java代码   收藏代码
  1. ******************************************************************************************************************  
  2. velocity template test:  
  3. string test-----------${user}-----------${number}-----------${latestProduct.url}-----------${latestProduct.name}  
  4. condition test-----------  
  5. #if ($user == "Big Joe")  
  6. list iterator-----------  
  7. #foreach( $aa in $list )  
  8. $aa  
  9. #end  
  10. #end  
  11. date test-----------${date}  


至此整个例子就结束了,以上只是最简单的介绍,当然这两种技术还有待我们的深入研究。这个例子只不过是比较直观的表现两种技术的使用而已 

而且如果想学习方法模板模式和工厂模式的同学可以下载代码看看 


出处:http://ahuaxuan.iteye.com/blog/71430

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值