• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting size of an array thru EL

 
Varun Ratra
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Can we get a size of an array by using EL? Or, using scriptlet is the only way to get it, when constructing a JSP?

regards,
 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great question.

EL expects objects to abide by Bean Law. But some of our most common Java classes don't follow the rules: String.length(), *Arrays*.length, and the Collection classes.size().

It's likely that your collection/array/string exists in a bean, whether you are keeping it in a Struts ActionForm or just a plain ol' Transfer Object. That means you can add a method to your bean for retrieving size of these items.

An example is given here, and yes it does seem "kludgy".
 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh yes, and as Satou mentions here, you can use fn:length as long as you are working with J2EE 1.4. Here's a decent Sun tutorial on how to use it.

The "kludgy" way is for 1.3, which you may end up having to implement in the real world. The entire world isn't using 1.4 yet and you have to make do with whatever you've got. :roll:
 
Varun Ratra
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Marc,

Thanks a lot for your help.

regards,
 
Arvind Sampath
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marc,

How about having a generic static utility function, say getLength(Object) in some utility class which will return the size of the collection/array/String ?



This function can be registerd in a tld flle and could be invoked from the jsp using EL.


Sounds decent...doesnt it ? :-)

Cheers,
Arvind
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic