You are viewing a plain text version of this content. The canonical link for it is here.
Posted to taglibs-user@tomcat.apache.org by Ian Tomey <Ia...@lombardrisk.com> on 2002/04/22 12:13:24 UTC

No access to size() method of array from EL - a solution

Hi all,

Been using the JSTL EL and am a bit miffed that I cannot access the
size of an array or list. I do think there should be an easy way to do
this in the EL. However, I needed it working so created a solution that
I thought I would share.

I added the following class:

public class SizeFacadeMap implements Map {

  public boolean containsKey( Object key ) {
     return key instanceof Collection;
  }

  public Object get( Object key ) {
      return new Integer( ((Collection) key).size() );
  }

  //.... implement rest of Map methods to just throw exception

}

Then, add an instance of this class as a property in the request scope
(or whatever) named _size, then the following will work :

${ _size[ myCollectionAttribute ] }

Which is a bit of hack but works nicely. Actually using a fake map is a
great way of accessing methods within objects.

Regards
Ian

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: No access to size() method of array from EL - a solution

Posted by Shawn Bayern <ba...@essentially.net>.
On Mon, 22 Apr 2002, Ian Tomey wrote:

> Then, add an instance of this class as a property in the request scope
> (or whatever) named _size, then the following will work :
> 
> ${ _size[ myCollectionAttribute ] }
> 
> Which is a bit of hack but works nicely. Actually using a fake map is
> a great way of accessing methods within objects.

Indeed, "fake maps" are actually what objects like ${param} and
${pageScope} are too.  :-)  Good thought; thanks for sharing that, Ian.

We were happy to tolerate, in the standard, "fake maps" that can be backed
up logically by relatively "real maps"; a standard probably shouldn't go
as far as the '_size' Map you propose, but it's a great thing for users to
try when they absolutely need the functionality.  Ultimately, in JSTL 1.1
or later, I expect EL functions (or as some advocate, direct method access
from the EL) will address 'size' determination.  Note that you can also
write loops like this:

 <c:forEach varStatus="s" items="${collection}">
   <c:if test="${s.last}">
     <c:set var="size" value="${s.count}"/>
   </c:if>
 </c:forEach>

However, these aren't efficient; they need to walk through the collection.

Also, the most common check ("size == 0") is now covered by the EL's
"empty" operator.  This is new to PFD and is the preferred way to test
against null, empty strings, and empty collections, all at once.  Thus:

  ${empty param.foo}     // no "foo" parameter (either null or "")
  ${empty myCollection}  // myCollection.size() == 0

-- 
Shawn Bayern
"JSP Standard Tag Library"   http://www.jstlbook.com
(coming this summer from Manning Publications)


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>