Reputation: 8426
I'm making heavy use of ArrayList in some JSP pages. I would like to access an ArrayList like so:
${myArrayList.size}
But since objects must to conform to the JavaBean standard, where myArrayList.getMyPropertyName()
is ${myArrayList.myPropertyName}
in JSP/JSTL, the myArrayList.size()
function is not accessible.
Is there another class I should be using?
Update:
It's a bit difficult to accept that a feature such as getting the size of your ArrayList in JSP is left out just for cleaner one liners but it seems there can be other reasons the designers chose .size()
instead of .getSize()
I guess this really isn't a problem if you have support for ${fn:length(myArrayList)}
or ${myArrayList.getSize()}
syntax.
Upvotes: 5
Views: 3591
Reputation: 597234
The latest version of EL supports method invocations ${collection.size()}
The other two answers give you more details
${fn:length(collection)}
By the way, in a JSP you shouldn't normally need the size()
of a collection - you just iterate it. If you have to perform some calculations, think of doing that in the servlet/controller. Of course, I agree, it is valid to use that sometimes in the jsp itself.
Upvotes: 1
Reputation: 183466
The title says it all: Why doesn't ArrayList have getSize() instead of size()?
As explained in "Java Collections API Design FAQ":
Why didn't you use "Beans-style names" for consistency?
While the names of the new collections methods do not adhere to the "Beans naming conventions", we believe that they are reasonable, consistent and appropriate to their purpose. It should be remembered that the Beans naming conventions do not apply to the JDK as a whole; the AWT did adopt these conventions, but that decision was somewhat controversial. We suspect that the collections APIs will be used quite pervasively, often with multiple method calls on a single line of code, so it is important that the names be short. Consider, for example, the Iterator methods. Currently, a loop over a collection looks like this:
for (Iterator i = c.iterator(); i.hasNext(); ) System.out.println(i.next());Everything fits neatly on one line, even if the Collection name is a long expression. If we named the methods "getIterator", "hasNextElement" and "getNextElement", this would no longer be the case. Thus, we adopted the "traditional" JDK style rather than the Beans style.
Upvotes: 11