Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JSF Render Attribute - Array Length

 
Cory Cowgill
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simple question which I can't find an answer to by searching the forums.

How do I use JSF EL Binding to get the length of an Array in a backing bean?

Example:

If the size of an Array is 0, I want to set the render attribute of a <h:dataTable> to false.

With a managed backing bean like this:

@ManagedBean
public class acctBean
{

public Account[] accts;

public Account[] getAccts() {
return accts;
}
..
}

I'd like to render/not render the datatable like this:

<h:dataTable class="acctTable" border="1" value="#{acctBean.accts}" var="item" rendered="#{acctBean.accts.length > 0}">

However, this throws an exception:

SEVERE: java.lang.NumberFormatException: For input string: "length"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)

Suggestions? Google isn't giving me a search result for an answer yet....


Thanks.
 
Stefan Evans
Bartender
Posts: 1755
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This link has some related answers in it
http://stackoverflow.com/questions/206161/how-do-you-get-the-length-of-a-list-in-the-jsf-expression-language


If you can use facelets, then you might be able to employ the #{fn:length(acctBean.accts)}


If you have a JEE6 container, and convert the array to a Collection, you should be able to invoke: #{acctBean.accts.size()> 0}

Otherwise the other solution is to add a workaround method to your backing bean which the EL can invoke.



and access it as #{acctBean.lengthOfArray}
 
Cory Cowgill
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Stefan!

I appreciate the feedback and the mutliple solutions.
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18218
53
Android Eclipse IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OR, you could just use simple EL.



The "empty" operator is fairly flexible. Depending on context, it can check for null objects, empty strings, or empty arrays.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic