How does EL empty operator work in JSF?

后端 未结 2 422
借酒劲吻你
借酒劲吻你 2020-11-28 05:33

In JSF an component can be rendered or not using the EL empty operator

rendered=\"#{not empty myBean.myList}\"

As I\'ve understood the ope

2条回答
  •  萌比男神i
    2020-11-28 06:27

    From EL 2.2 specification (get the one below "Click here to download the spec for evaluation"):

    1.10 Empty Operator - empty A

    The empty operator is a prefix operator that can be used to determine if a value is null or empty.

    To evaluate empty A

    • If A is null, return true
    • Otherwise, if A is the empty string, then return true
    • Otherwise, if A is an empty array, then return true
    • Otherwise, if A is an empty Map, return true
    • Otherwise, if A is an empty Collection, return true
    • Otherwise return false

    So, considering the interfaces, it works on Collection and Map only. In your case, I think Collection is the best option. Or, if it's a Javabean-like object, then Map. Either way, under the covers, the isEmpty() method is used for the actual check. On interface methods which you can't or don't want to implement, you could throw UnsupportedOperationException.

提交回复
热议问题