如:类别的父类(category.parentCategory)为空,freemarker中会出异常category.parentCategory is undefined的异常。
解决办法:
${(category.parentCategory.id)!0}
加括号代表category, category.parentCategory, category.parentCategory.id这三个都会进行判断
不加括号只判断category.parentCategory.id是否为null,如果category.parentCategory就为null了,最后还是会出现异常,所以要加上括号。感叹号后面的0是默认值,为null是值为0。
还有一种是
<#if list?exists && list.size != 0 >
注意:在freemarker中parent貌似是关键字(网上没找到),因为要是${category.parent} 这样即使也不为null,照样报异常,改成parentCategory就正常了,有待大家验证!