[英]In Velocity, how can I make the template render nulls, null pointers and index out of bound exceptions as blank?
我有一个速度模板,其中有一行如下:
$person.names.get(0).FullName
问题是如果names
为null,或者get(0)
返回null,则将其计算为文字
$person.names.get(0).FullName
相反,我希望这打印“”没有引号。 有没有办法配置Velocity以这种方式工作? 我不想把我做的所有东西都包起来
#if (x != null && x.somethingElse != null ...)
...
#end
我一直在看胡子 ,它似乎默认以这种方式工作。
VelocityContext context = new VelocityContext();
EventCartridge eventCartridge = new EventCartridge();
eventCartridge.addReferenceInsertionEventHandler(new ReferenceInsertionEventHandler() {
public Object referenceInsert(String reference, Object value) {
if (value == null) {
return StringUtils.EMPTY;
} else {
return value;
}
}
});
context.attachEventCartridge(eventCartridge);
索引超出范围时仍会出现错误。 我有一种感觉,没有办法避免这种情况。 如果有人能想出办法,我很乐意将其作为答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.