Home > JSF, Mini-tips > Get collection (set,list,map) size in JSF view (xhtml)

Get collection (set,list,map) size in JSF view (xhtml)

When I started with JSF  I needed to know the size of certain list, and the natural thinking was… well Im gonna try #{bean.myAwesomeList.size} of course it is not going to work, so… after this I met the JSTL Functions Library and tadaaaaa!! problem solved, I used the fn:length which gives you the size of any Collections or the length of a String, and it goes something like this:

Step 1:  Include in your html the xmlns of this library

<html 
    xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:h="http://java.sun.com/jsf/html"
    ...here goes all your namespaces...  
    xmlns:fn="http://java.sun.com/jsp/jstl/functions" />
 

Step 2:  Show the magic!

<h:outputText 
   value="My collection size: #{fn:length(bean.myList)} Dang thats huge!" />
Categories: JSF, Mini-tips
  1. Anonymous
    December 3, 2012 at 12:58

    java.lang.NullPointerException
    com.sun.facelets.el.CompositeFunctionMapper.resolveFunction(CompositeFunctionMapper.java:48)
    com.sun.facelets.el.CompositeFunctionMapper.resolveFunction(CompositeFunctionMapper.java:48)
    com.sun.facelets.el.CompositeFunctionMapper.resolveFunction(CompositeFunctionMapper.java:48)
    org.apache.el.lang.FunctionMapperFactory.resolveFunction(FunctionMapperFactory.java:48)

    • Anonymous
      December 3, 2012 at 13:00

      xmlns:fn=”http://java.sun.com/jsp/jstl/functions”

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: