• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Populating a html select tag without scripting

 
Ranch Hand
Posts: 751
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 751
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 751
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 751
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic