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 代码
-
-
-
- public class EmptyTemplateHashModel implements TemplateHashModel, TemplateScalarModel
- {
- public final static EmptyTemplateHashModel INSTANCE = new EmptyTemplateHashModel();
-
- public TemplateModel get(String n) throws TemplateModelException
- {
- return INSTANCE;
- }
-
- public boolean isEmpty() throws TemplateModelException
- {
- return true;
- }
-
- public String getAsString() throws TemplateModelException
- {
- return "";
- }
- }
-
-
-
-
-
- public class DelegatingTemplateHashModel implements TemplateHashModel
- {
- private TemplateHashModel target;
-
- public DelegatingTemplateHashModel(TemplateHashModel target)
- {
- this.target = target;
- }
-
- public TemplateModel get(String name) throws TemplateModelException
- {
- TemplateModel model = this.target.get(name);
- if (model == null)
- {
- return EmptyTemplateHashModel.INSTANCE;
- }
- if (model instanceof TemplateHashModel)
- {
- return new DelegatingTemplateHashModel((TemplateHashModel) model);
- }
- return model;
- }
-
- public boolean isEmpty() throws TemplateModelException
- {
- return this.target.isEmpty();
- }
- }
分享到:
- 2007-03-10 09:30
- 浏览 3470
- 评论(3)
- 论坛回复 / 浏览 (3 / 3465)
- 查看更多
相关推荐
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....
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来...
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. ...
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装载...
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. ...
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装载...
在丰富的示例中直观地探讨了许多实用的技术。如数据类型转换、文件上传和下载、Struts2应用的安全性、调试与性能分析、FreeMarker、Velocily、Ajax,等等。跟随作者一道深入Struts2。聆听大量来之不易的经验之谈。你...
作者处处从实战出发,在丰富的示例中直观地探讨了许多实用的技术,如数据类型转换、文件上传和下载、提高Struts 2应用的安全性、调试与性能分析、FreeMarker、Velocity、Ajax,等等。跟随作者一道深入Struts 2,聆听...