• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JSF Render Attribute - Array Length

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 1845
10
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Stefan!

I appreciate the feedback and the mutliple solutions.
 
Saloon Keeper
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic