This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes JSF and the fly likes JSF Render Attribute - Array Length Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "JSF Render Attribute - Array Length" Watch "JSF Render Attribute - Array Length" New topic
Author

JSF Render Attribute - Array Length

Cory Cowgill
Greenhorn

Joined: Mar 15, 2011
Posts: 4
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

Joined: Jul 06, 2005
Posts: 1018
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

Joined: Mar 15, 2011
Posts: 4
Thanks Stefan!

I appreciate the feedback and the mutliple solutions.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16019
    
  20

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.


Customer surveys are for companies who didn't pay proper attention to begin with.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JSF Render Attribute - Array Length