簡體   English   中英

使用EL對空屬性進行快速故障轉移的最簡單方法?

[英]Easiest way to fail-fast on an empty attribute using EL?

EL調用什么也不返回時,是否可以強制發生異常?

我喜歡EL提供的方便的$ {...}表示法,但我希望每次對pageContext.findAttribute(...)的調用 EL基本上是IIRC進行的操作)在沒有屬性的情況下拋出異常(或包含空字符串的屬性)。

我可以繼續使用EL還是應該使用其他東西?

一種選擇是創建自己的ELResolver 您可能可以擴展ScopedAttributeELResolver並覆蓋getValue

public Object getValue(ELContext context, Object base, Object property) {
    Object value = super.getValue(context, base, property);
    if (context.isPropertyResolved() && (value == null)) {
        throw new PropertyNotFoundException("Scoped attribute not found");
    }
    return value;
}

為了注冊解析器,創建一個ServletContextListener並在contextInitialized執行以下操作:

JspFactory.getDefaultFactory().getJspApplicationContext(sce).addELResolver(resolver);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM