This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread 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 Murach's Java Servlets and JSP this week in the Servlets 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: 1016
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: 15951
    
  19

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
 
Similar Threads
How to design a jsf page dynamically with input text,radio button etc
Rendering a List in a JSF page
Best pratices about DataModel, Datatable and Serialization?
question about treemaps
read words from file into array