在JSF中,组件可以使用EL空运算符呈现,也可以不呈现
rendered="#{not empty myBean.myList}"据我所知,操作符的工作方式既是null- check,也是check检查列表是否为空。
我想对我自己的自定义类的一些对象进行空检查,我需要实现哪些接口或接口的哪些部分?空运算符与哪个接口兼容?
发布于 2013-01-07 02:04:48
来自EL 2.2 specification (获取下面的“单击此处下载用于评估的规范”):
1.10空运算符-
empty A
empty运算符是一个前缀运算符,可用于确定值是null还是空。
评估empty A
A为null,则返回trueA为空字符串,则返回trueA为空数组,则返回trueA为空,则返回false;如果A为空<A>D27A>,则返回A
因此,考虑到接口,它只能在Collection和Map上工作。对于您来说,我认为Collection是最好的选择。或者,如果它是一个类似Javabean的对象,则使用Map。无论哪种方式,在幕后,isEmpty()方法都用于实际检查。对于你不能或不想实现的接口方法,你可以抛出UnsupportedOperationException。
发布于 2013-01-08 02:51:59
使用BalusC提出的实现集合的建议,我现在可以在扩展javax.faces.model.ListDataModel的dataModel上使用not empty操作符隐藏primefaces p:dataTable
代码示例:
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...
}https://stackoverflow.com/questions/14185031
复制相似问题