wood burning stoves 2.0*
The moose likes Struts and the fly likes To Display the Counts of a Collection that is Defined in a Form 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 » Frameworks » Struts
Bookmark "To Display the Counts of a Collection that is Defined in a Form" Watch "To Display the Counts of a Collection that is Defined in a Form" New topic
Author

To Display the Counts of a Collection that is Defined in a Form

Daniel Gee
Ranch Hand

Joined: Aug 29, 2003
Posts: 202
I am trying to display 'counts' in a Collection. And I use JSP scriptlet to do it:

The compiler cannot recognize the Collection 'businessCollection'. It is because businessCollection is defined in the ActivityForm.java.

When I tested whether businessCollection is empty or not, I used:

And it was successful.

Then, how do I display the counts of the Collection in this situation?

Thanks in advance.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
When you use a bean in a scriptlet, you have to pull it out of the scope in which it resides. Here is an example:

<%= ((com.mycompany.ActivityForm)request.getAttribute("ActivityForm")).getBusinessCollection().size() %>

This assumes ActivityForm is in request scope. If it's in session scope, use request.getSession().getAttribute(...)


Merrill
Consultant, Sima Solutions
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
This would probably work as well if you are just displaying the value on the screen:

<bean:write name="ActivityForm" property="businessCollection.size" />

- Brent
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Unfortunately the above will not work, as the size() method does not follow the javaBean standard. In other words, there is no getSize() method to go allong with a size property.

Fortunately, there is a tag just for getting the size of a collection. The following would work:

<bean:size name="ActivityForm" property="businessCollection"/>
[ March 09, 2006: Message edited by: Merrill Higginson ]
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
ahh yes...I have wondered if they ever planned on adding methods like getSize() and getIterator().

- Brent
 
Don't get me started about those stupid light bulbs.
 
subject: To Display the Counts of a Collection that is Defined in a Form
 
Similar Threads
Break out of a logic block using logic tags
CSS Float - how to "turn off" for subsequent content
Adding html tags to script
How to Introduce a Button That Controls All Other Links in a Web Page
Unable to locate tag attribute info for tag attribute value.