null check in jsf expression language

前端 未结 1 1200
庸人自扰
庸人自扰 2021-01-31 07:44

Please see this Expression Language

styleClass=\"#{obj.validationErrorMap eq null ? \' \' :  
     obj.validationErrorMap.contains(\'key\')?\'highlight_field\':\         


        
1条回答
  •  深忆病人
    2021-01-31 08:16

    Use empty (it checks both nullness and emptiness) and group the nested ternary expression by parentheses (EL is in certain implementations/versions namely somewhat problematic with nested ternary expressions). Thus, so:

    styleClass="#{empty obj.validationErrorMap ? ' ' :  
     (obj.validationErrorMap.contains('key') ? 'highlight_field' : 'highlight_row')}"
    

    If still in vain (I would then check JBoss EL configs), use the "normal" EL approach:

    styleClass="#{empty obj.validationErrorMap ? ' ' :  
     (obj.validationErrorMap['key'] ne null ? 'highlight_field' : 'highlight_row')}"
    

    Update: as per the comments, the Map turns out to actually be a List (please work on your naming conventions). To check if a List contains an item the "normal" EL way, use JSTL fn:contains (although not explicitly documented, it works for List as well).

    styleClass="#{empty obj.validationErrorMap ? ' ' :  
     (fn:contains(obj.validationErrorMap, 'key') ? 'highlight_field' : 'highlight_row')}"
    

    0 讨论(0)
提交回复
热议问题