在JSF中,可以使用EL空运算符渲染或不渲染组件
rendered="#{not empty myBean.myList}"
Run Code Online (Sandbox Code Playgroud)
正如我所理解的那样,运算符既可以作为空值检查,也可以检查列表是否为空.
我想对我自己的自定义类的某些对象进行空检查,我需要实现哪些接口或部分接口?哪个接口是空的运算符兼容?
Bal*_*usC 145
从EL 2.2规范(获得下面的"点击此处下载评估规范"):
1.10空操作员 -
empty A所述
empty操作者是一个前缀操作者可以被用来确定一个值是否为零或为空.评估
empty A
- 如果
A是null,请返回true- 否则,如果
A是空字符串,则返回true- 否则,如果
A是一个空数组,则返回true- 否则,如果
A是空的Map,则返回true- 否则,如果
A是空的Collection,则返回true- 否则返回
false
因此,考虑到接口,它的工作原理上Collection和Map只.在你的情况下,我认为Collection是最好的选择.或者,如果它是类似Javabean的对象,那么Map.无论哪种方式,在封面下,该isEmpty()方法用于实际检查.在您不能或不想实现的接口方法上,您可以抛出UnsupportedOperationException.
使用实施征收BalusC的建议,我现在可以隐藏我的primefaces p:dataTable使用不是空的经营者本人的dataModel延伸javax.faces.model.ListDataModel
代码示例:
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...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
121762 次 |
| 最近记录: |