简体   繁体   English

EL空运算符在JSF中如何工作?

[英]How does EL empty operator work in JSF?

In JSF an component can be rendered or not using the EL empty operator 在JSF中,可以使用EL空运算符来渲染或不渲染组件

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. 据我了解,该运算符既可以作为null检查,也可以检查列表是否为空。

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? 空运算符与哪个接口兼容?

From EL 2.2 specification (get the one below "Click here to download the spec for evaluation"): 根据EL 2.2规范 (请在“单击此处下载该规范以进行评估”下方获得该规范):

1.10 Empty Operator - empty A 1.10 empty A算符- empty A

The empty operator is a prefix operator that can be used to determine if a value is null or empty. empty算符是前缀运算符,可用于确定值是否为null或为空。

To evaluate empty A 评估empty A

  • If A is null , return true 如果Anull ,则返回true
  • Otherwise, if A is the empty string, then return true 否则,如果A是空字符串,则返回true
  • Otherwise, if A is an empty array, then return true 否则,如果A是一个空数组,则返回true
  • Otherwise, if A is an empty Map , return true 否则,如果A是一个空Map ,则返回true
  • Otherwise, if A is an empty Collection , return true 否则,如果A是一个空的Collection ,则返回true
  • Otherwise return false 否则返回false

So, considering the interfaces, it works on Collection and Map only. 因此,考虑到接口,它仅适用于CollectionMap In your case, I think Collection is the best option. 就您而言,我认为Collection是最佳选择。 Or, if it's a Javabean-like object, then Map . 或者,如果它是类似Javabean的对象,则使用Map Either way, under the covers, the isEmpty() method is used for the actual check. 无论哪种方式,在实际情况下,都使用isEmpty()方法进行实际检查。 On interface methods which you can't or don't want to implement, you could throw UnsupportedOperationException . 在您无法或不想实现的接口方法上,可以抛出UnsupportedOperationException

Using BalusC's suggestion of implementing Collection i can now hide my primefaces p:dataTable using not empty operator on my dataModel that extends javax.faces.model.ListDataModel 使用BalusC提出的实现Collection的建议,我现在可以在扩展javax.faces.model.ListDataModel dataModel上使用非空运算符隐藏我的素面p:dataTable

Code sample: 代码示例:

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import javax.faces.model.ListDataModel;
import org.primefaces.model.SelectableDataModel;

public class EntityDataModel extends ListDataModel<Entity> implements
        Collection<Entity>, SelectableDataModel<Entity>, Serializable {

    public EntityDataModel(List<Entity> data) { super(data); }

    @Override
    public Entity getRowData(String rowKey) {
        // In a real app, a more efficient way like a query by rowKey should be
        // implemented to deal with huge data
        List<Entity> entitys = (List<Entity>) getWrappedData();
        for (Entity entity : entitys) {
            if (Integer.toString(entity.getId()).equals(rowKey)) return entity;
        }
        return null;
    }

    @Override
    public Object getRowKey(Entity entity) {
        return entity.getId();
    }

    @Override
    public boolean isEmpty() {
        List<Entity> entity = (List<Entity>) getWrappedData();
        return (entity == null) || entity.isEmpty();
    }
    // ... other not implemented methods of Collection...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM