簡體   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