繁体   English   中英

在Velocity中,如何使模板呈现空值,空指针和索引超出绑定的异常为空?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM