J2EE 学习

java struts1、struts2 spring hibernate freemarker ajax fckeditor Mysql MSSQL ORACLE DB2 Websphere jboss
随笔 - 11, 文章 - 13, 评论 - 2, 引用 - 0
数据加载中……

freemarker中空值的多种处理方法

1.按照freemarker的规范,老老实实的判断是否有空值,有空值怎么处理。这在某种时候是有用的。
格式:${empty!"EmptyValue of fbysss"}
比如值为空时,你可以给出一个友好的说明,但是很多的变量都要这么说明,未免太麻烦了。

2.<#escape x as x!""></#escape>可以对所有的变量进行空值处理,这里是全部替换为空字符串。当然也可以替换为其它字符串。
如果其中某些变量不需要这种替换,可以加入<#noescape></#noescape>标签。

3.属性配置方法:
配置classic_compatible=true可以满足一般需要。默认情况变量为null则替换为空字符串,如果需要自定义,写上${empty!"EmptyValue of fbysss"}的形式即可

a.通过Configuration设置。Configuration cfg = new Configuration(); cfg.setClassicCompatible(true);//设置属性

b.通过Eviroment设置。
   Environment env = template.createProcessingEnvironment(root, out);
   env.setClassicCompatible(true);

c.通过ftl设置:在ftl前加入<!--#setting classic_compatible=true-->;

d.通过Spring配置文件设置
<bean id="freemarkerConfig"
    class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
  <property name="freemarkerSettings">
    <props>
      <prop key="classic_compatible">true</prop>
    </props>
  </property>
</bean>

e.class目录下添加freemarker.properties文件:加入classic_compatible=true
(需要struts2或spring)

posted on 2009-08-11 20:59 李峰 阅读(1487) 评论(0)  编辑  收藏 所属分类: j2ee


只有注册用户登录后才能发表评论。


网站导航: