• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Should I go for a Bean or an array or map

 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Christophe. Now the things are clear somewhat. If I get doubts again I will return.

Thank you.
 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you David.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic