`
ayufox
  • 浏览: 273756 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

在FreeMarker3.8-版本中实现FreeMarker3.8+的!功能

阅读更多
       FreeMarker3.8中引入了一个非常实用的!功能,在FreeMarker3.8-中,如果要显示类似${user.phone.areacode}的值,需要层层判断是否为NULL,譬如如上的表达式一般在FTL中需要写成<#if user?exists><#if user.phone?exists><#if user.phone.areacode?exists>${user.phone.areacode}},不胜烦琐,而在FreeMarker3.8中可以使用${(user.phone.areacode)!}达到同样的目的。那么如果使用的是FreeMarker3.8-,有什么可选的替代方案呢?下面是我想的一种解决方案

java 代码
 
  1. /** 
  2.  * @author Ray (ayufox@gmail.com) 
  3.  */  
  4. public class EmptyTemplateHashModel implements TemplateHashModel, TemplateScalarModel  
  5. {  
  6.     public final static EmptyTemplateHashModel INSTANCE = new EmptyTemplateHashModel();  
  7.   
  8.     public TemplateModel get(String n) throws TemplateModelException  
  9.     {  
  10.         return INSTANCE;  
  11.     }  
  12.   
  13.     public boolean isEmpty() throws TemplateModelException  
  14.     {  
  15.         return true;  
  16.     }  
  17.   
  18.     public String getAsString() throws TemplateModelException  
  19.     {  
  20.         return "";  
  21.     }  
  22. }  
  23.          
  24.       
  25. /** 
  26.  * @author Ray (ayufox@gmail.com) 
  27.  */  
  28. public class DelegatingTemplateHashModel implements TemplateHashModel  
  29. {  
  30.     private TemplateHashModel target;  
  31.   
  32.     public DelegatingTemplateHashModel(TemplateHashModel target)  
  33.     {  
  34.         this.target = target;  
  35.     }  
  36.   
  37.     public TemplateModel get(String name) throws TemplateModelException  
  38.     {  
  39.         TemplateModel model = this.target.get(name);  
  40.         if (model == null)  
  41.         {  
  42.             return EmptyTemplateHashModel.INSTANCE;  
  43.         }  
  44.         if (model instanceof TemplateHashModel)  
  45.         {  
  46.             return new DelegatingTemplateHashModel((TemplateHashModel) model);  
  47.         }  
  48.         return model;  
  49.     }  
  50.   
  51.     public boolean isEmpty() throws TemplateModelException  
  52.     {  
  53.         return this.target.isEmpty();  
  54.     }  
  55. }  

    
分享到:
评论
3 楼 ayufox 2007-03-10  
受教,看来对freemarker了解的太少
2 楼 Readonly 2007-03-10  
N个版本以前就可以这样写了:
${(user.phone.areacode)?if_exists}

新版本只不过是把?if_eixsts替换成了!
1 楼 codeutil 2007-03-10  


FreeMarker是2.3.8吧? 现在最新版本是2.3.9.

相关推荐

    springmvc 各种jar包

    20,freemarker-2.3.20.jar 21,hamcrest-all-1.3.jar 22,hibernate-3.6.10.jar 23,hibernate-jpa-2.0-api-1.0.1.jar 24,hibernate-search-3.4.2.jar 25,hibernate-validator-4.2.0.jar 26,httpclient-4.2.3....

    Spring-Reference_zh_CN(Spring中文参考手册)

    6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.1.1. @Configurable object的单元测试 6.8.1.2. 多application context情况下的处理 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来...

    spring chm文档

    6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ Load-time weaving(LTW) 6.9. ...

    Spring中文帮助文档

    2.2.5. 在classpath中自动搜索组件 2.3. 面向切面编程(AOP) 2.3.1. 更加简单的AOP XML配置 2.3.2. 对@AspectJ 切面的支持 2.3.3. 对bean命名pointcut( bean name pointcut element)的支持 2.3.4. 对AspectJ装载...

    Spring 2.0 开发参考手册

    6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ Load-time weaving(LTW) 6.9. ...

    Spring API

    2.2.5. 在classpath中自动搜索组件 2.3. 面向切面编程(AOP) 2.3.1. 更加简单的AOP XML配置 2.3.2. 对@AspectJ 切面的支持 2.3.3. 对bean命名pointcut( bean name pointcut element)的支持 2.3.4. 对AspectJ装载...

    深入浅出Struts 2 .pdf(原书扫描版) part 1

    在丰富的示例中直观地探讨了许多实用的技术。如数据类型转换、文件上传和下载、Struts2应用的安全性、调试与性能分析、FreeMarker、Velocily、Ajax,等等。跟随作者一道深入Struts2。聆听大量来之不易的经验之谈。你...

    深入浅出Struts2(附源码)

    作者处处从实战出发,在丰富的示例中直观地探讨了许多实用的技术,如数据类型转换、文件上传和下载、提高Struts 2应用的安全性、调试与性能分析、FreeMarker、Velocity、Ajax,等等。跟随作者一道深入Struts 2,聆听...

Global site tag (gtag.js) - Google Analytics