[Freemarker] Freemarker 处理XML时的 null-safe问题(例示)

设XML有三层, grandfather.father.son, 其中son元素包含了一个字符串,比如 <son>John<son>.

如果 father是必填,而son都非必填,那么我们如何安全地显示 son 的值?

答:

  ${grandfather.father.son[0]!} 

如果father和son都不是必填的呢?

答:

   <#if grandfather.father[0] ??>
          ${grandfather.father.son[0]!}  
   </#if>

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.