120

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

#{MyBean.somelist.length}

does not work.

Beryllium
  • 12,808
  • 10
  • 56
  • 86
GBa
  • 17,509
  • 15
  • 49
  • 67
  • 7
    Related: [How to display value of List#size() in JSF EL?](http://stackoverflow.com/questions/7120526/how-to-display-value-of-listsize-in-jsf-el) – BalusC Aug 23 '11 at 15:25
  • 1
    @GBa you can use #{MyBean.somelist.size()} in order to display size of list. – Umair Aug 06 '14 at 05:45
  • You could also use c:forEach to loop over the list and in the loop body assign the count value to a variable, like: . Useful especially when you are looping over the list anyway. – David Balažic Jan 06 '15 at 13:18

7 Answers7

172

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) }
Beryllium
  • 12,808
  • 10
  • 56
  • 86
billjamesdev
  • 14,554
  • 6
  • 53
  • 76
  • 2
    fn is probably the way to go, unfortunately it is only available in JSTL versions greater then 1.1. – James McMahon Jan 27 '10 at 13:36
  • 27
    Using fn:length worked for me with bare JSP (no JSF, Facelets; ultra legacy project being lightly updated before being phased out). The proper taglib to use: `<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>` – Christopher Parker Feb 17 '10 at 20:01
  • 59
    For people landing here like me, still 2 year latter, considere you may add the following namespace to your facelet: `xmlns:fn="http://java.sun.com/jsp/jstl/functions"` – Damien MIRAS Jul 15 '11 at 08:47
64

You mean size() don't you?

#{MyBean.somelist.size()}

works for me (using JBoss Seam which has the Jboss EL extensions)

Damo
  • 11,410
  • 5
  • 57
  • 74
  • Maybe the newer specifications of the EL support the size method? Can anyone confirm or deny this? – James McMahon Sep 01 '09 at 11:29
  • 2
    @nemo - this is being addressed in a JSR245 maintenance release: http://blogs.sun.com/kchung/entry/jsr_245_mr_part_i (In terms of JEE specs, that'll be JEE6) – McDowell Sep 01 '09 at 11:46
  • @McDowell, Thanks for the info, I'm really surprised it took them this long to get around to fixing the issue. – James McMahon Sep 01 '09 at 12:11
  • Thank you. It worked for me too. +1. But until JEE 6 is widely supported, I'll probably stick with the "more standard" JSTL function. – Hosam Aly Sep 02 '09 at 05:08
  • BTW, it probably works in JBoss to just say `#{MyBean.somelist.size}` (without the parenthesis at the end). – Hosam Aly Sep 08 '09 at 10:04
  • With Jboss EL if you leave out the parenthesis it tries to find getSize() or isSize() – Damo Sep 08 '09 at 10:10
  • We're on JEE 6, and while `size` works fine for e.g. `ArrayList` etc., neither `size` nor `length` are defined for `Object[]`, so I find myself still using `fn:length(...)` – Antares42 Jan 13 '15 at 14:06
  • Eclipse complains: "Syntax error in EL"... but it works :D – Marco Sulla Jul 23 '21 at 13:25
14

Note: This solution is better for older versions of JSTL. For versions greater then 1.1 I recommend using fn:length(MyBean.somelist) as suggested by Bill James.


This article has some more detailed information, including another possible solution;

The problem is that we are trying to invoke the list's size method (which is a valid LinkedList method), but it's not a JavaBeans-compliant getter method, so the expression list.size-1 cannot be evaluated.

There are two ways to address this dilemma. First, you can use the RT Core library, like this:

<c_rt:out value='<%= list[list.size()-1] %>'/>

Second, if you want to avoid Java code in your JSP pages, you can implement a simple wrapper class that contains a list and provides access to the list's size property with a JavaBeans-compliant getter method. That bean is listed in Listing 2.25.

The problem with c_rt method is that you need to get the variable from request manually, because it doesn't recognize it otherwise. At this point you are putting in a lot of code for what should be built in functionality. This is a GIANT flaw in the EL.

I ended up using the "wrapper" method, here is the class for it;

public class CollectionWrapper {

    Collection collection;

    public CollectionWrapper(Collection collection) {
        this.collection = collection;
    }

    public Collection getCollection() {
        return collection;
    }

    public int getSize() {
        return collection.size();
    }
}

A third option that no one has mentioned yet is to put your list size into the model (assuming you are using MVC) as a separate attribute. So in your model you would have "someList" and then "someListSize". That may be simplest way to solve this issue.

Community
  • 1
  • 1
James McMahon
  • 48,506
  • 64
  • 207
  • 283
  • also http://stackoverflow.com/questions/1332169/how-do-you-call-list-size-from-a-jsf-2-0-facelets-template – bora.oren Jan 09 '12 at 22:09
7
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>

<h:outputText value="Table Size = #{fn:length(SystemBean.list)}"/>

On screen it displays the Table size

Example: Table Size = 5

UdayKiran Pulipati
  • 6,579
  • 7
  • 67
  • 92
  • Yes this I find is the simplest way of doing it. I'm using websphere6 with JSP (legacy system). used the same tablib and then use the 'dollar sign' as opposed to 'hash' because hash didn't work. ie: {fn:length(SystemBean.list)} – JackDev Aug 13 '13 at 07:19
3

After 7 years... the facelets solution still works fine for me as a jsf user

include the namespace as xmlns:fn="http://java.sun.com/jsp/jstl/functions"

and use the EL as #{fn:length(myBean.someList)} for example if using in jsf ui:fragment below example works fine

<ui:fragment rendered="#{fn:length(myBean.someList) gt 0}">
    <!-- Do something here-->
</ui:fragment>
Arry
  • 107
  • 1
  • 4
0

You can eventually extend the EL language by using the EL Functor, which will allow you to call any Java beans methods, even with parameters...

Romain Linsolas
  • 79,475
  • 49
  • 202
  • 273
-6

You can get the length using the following EL:

#{Bean.list.size()}

Soujanya
  • 25
  • 1
  • 1
    This is essentially the same answer as [Damo's](http://stackoverflow.com/a/208093/1816580). – Artjom B. Jul 29 '14 at 19:47
  • 6
    As @ArtjomB. sugggests, it's not generally good here to post what amounts to the same answer as someone else already posted. In this case, almost **six years ago**. – Andrew Barber Jul 29 '14 at 19:52