EL 空运算符在 JSF 中如何工作? [英] How does EL empty operator work in JSF?

查看:18
本文介绍了EL 空运算符在 JSF 中如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 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 或空.

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.

评估空A

  • 如果Anull,返回true
  • 否则,如果A为空字符串,则返回true
  • 否则,如果A是一个空数组,则返回true
  • 否则,如果A是一个空的Map,则返回true
  • 否则,如果A是一个空的Collection,则返回true
  • 否则返回false
  • 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

因此,考虑到接口,它适用于 CollectionMap 仅.在你的情况下,我认为 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 中如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆