How do you get the length of a list in the JSF expression language?

GBa picture GBa · Oct 15, 2008 · Viewed 128k times · Source

How would I get the length of an ArrayList using a JSF EL expression?

#{MyBean.somelist.length}

does not work.

Answer

billjamesdev picture billjamesdev · Oct 15, 2008

Yes, since some genius in the Java API creation committee decided that, even though certain classes have size() members or length attributes, they won't implement getSize() or getLength() which JSF and most other standards require, you can't do what you want.

There's a couple ways to do this.

One: add a function to your Bean that returns the length:

In class MyBean:
public int getSomelistLength() { return this.somelist.length; }

In your JSF page:
#{MyBean.somelistLength}

Two: If you're using Facelets (Oh, God, why aren't you using Facelets!), you can add the fn namespace and use the length function

In JSF page:
#{ fn:length(MyBean.somelist) }