问题描述
在 JSF 中,可以使用 EL 空运算符呈现或不呈现组件
In JSF an component can be rendered or not using the EL empty operator
rendered="#{not empty myBean.myList}"
据我所知,该运算符既可用作空检查,也可检查列表是否为空.
As I've understood the operator works both as null-check, but also check checks if the list is empty.
我想对我自己的自定义类的某些对象进行空检查,我需要实现哪些接口或部分接口?空运算符与哪个接口兼容?
I want to do empty checks on some objects of my own custom class, which interface(s) or parts of interfaces do I need to implement?Which interface is the empty operator compatible with?
推荐答案
来自 EL 2.2 规范(获取下面的点击此处下载评估规范"):
From EL 2.2 specification (get the one below "Click here to download the spec for evaluation"):
empty
运算符是一个前缀运算符,可用于确定值是否为null 或空.
评估空A
- 如果
A
为null
,返回true
- 否则,如果
A
为空字符串,则返回true
- 否则,如果
A
是一个空数组,则返回true
- 否则,如果
A
是一个空的Map
,则返回true
- 否则,如果
A
是一个空的Collection
,则返回true
- 否则返回
false
- If
A
isnull
, returntrue
- Otherwise, if
A
is the empty string, then returntrue
- Otherwise, if
A
is an empty array, then returntrue
- Otherwise, if
A
is an emptyMap
, returntrue
- Otherwise, if
A
is an emptyCollection
, returntrue
- Otherwise return
false
因此,考虑到接口,它适用于 Collection代码>
和Map
仅.在你的情况下,我认为 Collection
是最好的选择.或者,如果它是一个类似 Javabean 的对象,则使用 Map
.无论哪种方式,在幕后,isEmpty()
方法用于实际检查.在您不能或不想实现的接口方法上,您可以抛出 UnsupportedOperationException
.
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
.
这篇关于EL 空运算符在 JSF 中如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!