Meaningless Drivel is fun!*
The moose likes Servlets and the fly likes Should I go for a Bean or an array or map Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Should I go for a Bean or an array or map" Watch "Should I go for a Bean or an array or map" New topic
Author

Should I go for a Bean or an array or map

chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 800

Hi everybody, I am Chaitanya, I am getting confused whether to use a Bean or a Map.

My aim is to select user profile data from database. For this I used MVC. In model, should I use a Bean and set all the profile data or should I create a Map and return the Map to controller and the add the Map to response object and print the details in jsp.

Which way is better?

Thank you all in advance. Have a nice day.


Love all, trust a few, do wrong to none.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

The model is generally represented by its own class, not by a Map. It gives you more control on what really is in the model.


[My Blog]
All roads lead to JavaRanch
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 800

Christophe Verré wrote:The model is generally represented by its own class, not by a Map. It gives you more control on what really is in the model.

I did not understand
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

If you use a Map, you can put anything in it. But you don't have a clear view of what the model is. All you see is : Map<String, Object> myModel. What's in it ? You don't know. until you loop through it. If you use a bean, you clearly see which properties it has, which relations it has with other beans... You clearly see that a Person has a first name, a last name... With a Map, you don't.
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 800

Thank you Christophe, but here I have to create another class for Bean, there will be plenty of code I think.

With Bean
========
controller
model
Bean
view(jsp)

With Map
========
controller
model
map
view

This will become easy I suppose. What do you say. If this is wrong approach please let me know.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Nowadays, beans used as the model are closely related to database entities. They are used to persist your data into a database. You may have a Person bean which is related to a Person table, a Company bean which has its properties stored in a Company table... Using a map for everything is not going to help you do that easily. If you start playing with JPA or persistence containers like Hibernate, you'll see that using a separate class is a must.

A part from that, from a design point of view, as I said earlier, a Map hides everything. Imagine that someone of your team is making a few JSP to add/update/delete/list some of your model. How will he know which properties your model is holding if you are using a Map. A Map is fine for playing alone But not maintainable.
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 800

Thank you Christophe. Now the things are clear somewhat. If I get doubts again I will return.

Thank you.
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 800

I am doing like this.

The use will click on a link named viewProfile. An appropriate servlet is called. Then that servlet sends the user_id to the model. The model selects the details from database and creates a person bean, then the controller adds the bean to response and calls user_details.jsp.

Is this right way?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Yes (with the caveat that we don't actually *know* what you're doing), it sounds like you're on the right path.
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 800

Thank you David.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Should I go for a Bean or an array or map
 
Similar Threads
The granularity of JSF beans
Why use Map.entrySet()
General Architecture question
Is there anyone who uses entity bean in his/her real enterprise project?
JSTL