aspose file tools*
The moose likes JSP and the fly likes Populating a html select tag without scripting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Populating a html select tag without scripting" Watch "Populating a html select tag without scripting" New topic
Author

Populating a html select tag without scripting

Timothy Sam
Ranch Hand

Joined: Sep 18, 2005
Posts: 746
Hi, I'm trying to follow the model 2 dicipline...

Please consider taking at look at the following query I have to create and put in my JSP page's <select><option value=""></option></select> portion...

SELECT department_id, department_name FROM departments_tbl;

this would give me more than 1 result... I was wondering if I should create a servlet that returns Vector/ArrayList... Or a bean that has a get method that returns a vector? I'm a little confused... I've already been working overnight... I probably even sound senseless to you guys...


SCJP 1.5
http://devpinoy.org/blogs/lamia/ - http://everypesocounts.com/
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61187
    
  66

Select options usually have two elements: a value and display text:



such as the id and name that you are getting from your query.

Whenever I have to get select options from a database, I create a model class (a class which has no knowledge whatsoever of what kind of UI the program has) that returns a List of elements. Since each element needs two values, I created a class that implements the Map.Entry interface, and populate each one of these with the option value (as the Map.Entry key) and the option display text (as the Map.Entry value) before placing it in the List.

My controller servlet will call this model class to get the List of these elements, which it places on the request as a scoped variable. The JSP page then can use the JSTL and EL to construct the select element from the List (actually I do this so often I wrote a custom action to do it for me).
[ January 13, 2006: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61187
    
  66

Important concepts from above:

1) The class that interacts with the database has no idea that it's being used in a servlet/JSP environment.

2) The JSP has no idea where the data came from. All it knows is that there will be a scopded variable that is a List of Map.Entry items.

3) The servlet controller is the "middle man" who knows how to call the model class, and pass the results to the JSP.
Timothy Sam
Ranch Hand

Joined: Sep 18, 2005
Posts: 746
Hi Bear... Thanks! I understand you might be trying to make me learn by myself by allowing me to research on my own... I think I have quite understood your point... But there are still some things in Java that I actually am not well aware of such Map.Entry and I don't know if I have the time to learn it on my own. So far I am familliar with Vector, ArrayList and HashTable... I haven't even used a map yet... I would really appreciate if you could pull me off some code that demonstrate how I could use it with an option tag. Thanks a lot for your help man. And sorry for the bother...

P.S. I'm trying meet the deadline for my project... And I'm no student so you won't have to worry.
[ January 13, 2006: Message edited by: Timothy Sam ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61187
    
  66

I'm more than willing to help you write your own code, but I cannot write the code for you. If implementing a simple interface like Map.Entry has you spooked, you can create your own class from scratch that contains the two values to put into the List. And you have already stated that you are familiar with ArrayList, so I believe you have all the tools you need at your disposal to tackle this task.

Give it a shot and check back with any brick walls that you might hit.
Timothy Sam
Ranch Hand

Joined: Sep 18, 2005
Posts: 746
Ok dude, I understand your point. I'll try to come back with some good results... Hopefully next week. I appreciate your help, thanks again.
Timothy Sam
Ranch Hand

Joined: Sep 18, 2005
Posts: 746
Hi bear,

I was wondering about your suggestion... Should I create a new instance of a Map (HashMap in my case) each time I add it to a List(ArrayList)? Like...





I haven't implemented this... But I'm thinking of doing this in one of my bean's setter method... Thanks...

Oh and I don't plan to make the query in my bean.. I was just making an assumption...
[ January 16, 2006: Message edited by: Timothy Sam ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Timothy Sam:




One thing you don't want to do is re-instanciate that map with every iteration of your while loop.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
 
Don't get me started about those stupid light bulbs.
 
subject: Populating a html select tag without scripting