wood burning stoves 2.0*
The moose likes JSP and the fly likes Using Beans from Collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Using Beans from Collection" Watch "Using Beans from Collection" New topic
Author

Using Beans from Collection

Baruch Sadogursky
Ranch Hand

Joined: Apr 09, 2002
Posts: 62
Dear friends!
I'd like to use beans to build a table from their properties. The beans come in Collection, which is passed in HTTPServletRequest. So far so good.
Now I run a loop on the collection to get the references to the beans:

The bean instance doesn't have a name, since it's resides in the Collection. How can I reffer to it?


Regards,<br />Baruch.<p>SDFWOF<br />FGEHWS<br />FNEVGE
Winston Smith
Ranch Hand

Joined: Jun 06, 2003
Posts: 136
You may not want to use each class as a Bean in itself. You can have one single Bean to act as a Manager, and this Manager maintains the Collection. Therefore, the Manager will be used to create an Iterator for your Collection and use the Iterator to reference each object in the collection.
[ April 13, 2004: Message edited by: Winston Smith ]

for (int i = today; i < endOfTime; i++) { code(); }
Baruch Sadogursky
Ranch Hand

Joined: Apr 09, 2002
Posts: 62
That means - discard the beans idea and work in scriptlet, which is undesirable in JSP, as I know. Maybe there is a way to minimize the Java code?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61105
    
  66

You can avoid scriptlets on the page via the JSTL, which is collection-aware.
Backing up a bit... if the collection is not the best way for your page to be dealing with your beans, why is your controller setting things up that way? The controller should arrange the data in the manner best-suited for the page.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Baruch Sadogursky
Ranch Hand

Joined: Apr 09, 2002
Posts: 62
How esle can I pass a set of beans, which is build dinamically from the model domain?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61105
    
  66

How are they used on the page? Individually? As an ordered set?
If they are individual, a collection may not be the best manner.
If they are indeed a set, then passing them across as a collection and iterating over them (with the <c:forEach> JSTL tag if you want to avoid scriptlets) makes perfect sense.
Baruch Sadogursky
Ranch Hand

Joined: Apr 09, 2002
Posts: 62
The page iterates over the set, and displays the beans as rows in table.
Your sollution seems the one I look for
10x!
One little problem - I don't know JSTL Good reason to learn!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61105
    
  66

I don't know JSTL Good reason to learn!

I concur. Even though it seems like a small thing -- after all, it's simply notation -- I find pages using the JSTL rather than scriptlets to be incredibly easier to read and maintain.
Winston Smith
Ranch Hand

Joined: Jun 06, 2003
Posts: 136

You may not want to use each class as a Bean in itself.

What I mean here is, every class you create need not function as a Bean. Certainly you do not want to create classes in your JSP. For instance, on a recent project, I have a WebEntity Bean which models a supervisor/upper management. Now this supervisor will have numerous employees under his/her supervision. Each employee is modeled by the Employee class. Now, the Employee class is not meant to work as a Bean, however, the WebEntity class maintains a HashMap of Employee objects, so, in the web environment, you will have access to and may iterate through, the Collection of Employee objects, even though they are not Beans.
I use the above model often, as many classes need not function explicitly in the web environment, while others (the Beans), are made to work in the web environment.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61105
    
  66


many classes need not function explicitly in the web environment

The problem with that approach is that since the various JSP mechanisms (such as the built-in JSP actions, as well as the JSTL and the expression language) are meant to be used with bean-patterned classes. Exposing non-bean classes to the page forces the use of scriptlets and scriptlet expressions to access the methods of the class.
In order to make best use of the built-in JSP mechanisms (highly recommended by this bartender), you should write your page-exposed classes to follow the bean pattern. Where that is not possible, you could wrap your non-bean classes in bean-patterned wrapper classes for use on the page, or utilize bean-patterned delegates to allow easy access without resorting to on-page Java.
Baruch Sadogursky
Ranch Hand

Joined: Apr 09, 2002
Posts: 62
I'd like to use beans for convinient output of the properties using
.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Using Beans from Collection