9

What is the syntax for calling list.size() in a JSF 2.0 Facelets template (eg, using an h:outputText element)?

jsight
  • 27,819
  • 25
  • 107
  • 140
  • Possible duplicate: http://stackoverflow.com/questions/206161/jsf-list-length – seth Aug 26 '09 at 03:44
  • @seth - While that does contain one reasonable answer (that should work with JSF 2.0, though the answer predates it), I'm asking this question specifically to find out if there are any updates to JSF 2.0 that could affect the solution. :) – jsight Aug 26 '09 at 04:15

4 Answers4

23

How about this:

<h:outputText value="#{fn:length(someBean.someList)}" />

You'll need to reference the functions taglib in your JSF page (URI: http://java.sun.com/jsp/jstl/functions).

harto
  • 89,823
  • 9
  • 47
  • 61
7

Just try #{myBean.myList.size()}

Jérôme Verstrynge
  • 57,710
  • 92
  • 283
  • 453
André
  • 71
  • 1
  • 1
  • 2
    That wouldn't work with standard el. There are el extensions though (such as jboss el) that allow this notation. – Joeri Hendrickx Aug 04 '10 at 14:10
  • Apparently that's not the case (to my astonishment): see http://community.jboss.org/thread/173971 – Kawu Oct 25 '11 at 22:27
5

It's not a problem with JSF 2.0 exactly. The JSF spec still requires a class that conforms to the bean spec. The collection classes don't do that with the .size() method. It would still have to be .getSize(). Given the need for a "standard" way to access the class values, we're not getting around that in JSF any time soon. Somebody needs to fix the collection classes.

Jim Barrows
  • 3,634
  • 1
  • 25
  • 36
  • IC... I was hoping that they had reworked the expression syntax to allow method calls. It could be abused, but it would easily work around cases like this. :) I still don't understand why the collection class designer chose to do things this way. – jsight Aug 28 '09 at 02:41
2

Note: The exact namespace for "fn" is added like this:

<html xmlns="http://www.w3.org/1999/xhtml"
      ...
      xmlns:fn="http://java.sun.com/jsp/jstl/functions">
Benny Code
  • 51,456
  • 28
  • 233
  • 198