Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does EL empty operator work in JSF?

Tags:

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?

like image 727
Aksel Willgert Avatar asked Jan 06 '13 18:01

Aksel Willgert


People also ask

What is El in JSF?

Expression Language (EL), is a scripting language that's seen adoption within many Java frameworks, such as Spring with SpEL and JBoss with JBoss EL. In this article, we'll focus at the JSF's implementation of this scripting language – Unified EL.

Which symbol is used in JSF expression language?

The pound sign is used in JSF components because we want the expression to be evaluated by the JSF lifecycle and not by the JSP or Facelets engine.


2 Answers

From EL 2.2 specification (get the one below "Click here to download the spec for evaluation"):

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.

To evaluate empty A

  • 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

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.

like image 96
BalusC Avatar answered Sep 24 '22 01:09

BalusC


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

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... } 
like image 32
Aksel Willgert Avatar answered Sep 26 '22 01:09

Aksel Willgert