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)