• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to fill ListBox Dynamically from DataBase in JSP

 
Bhavik Patel
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HIiiiiiiii all ....i have a function known as search courses that will pop up the List Box dynamically from the SQL query...I have my coursebean that has the ArrayList courses as member variable that will capture the courses for specified semester and Major from the query.I will have the courses in ArryList can i use that variable with <select> tag and fill the option values by traversing a loop for Arraylist.
[ April 16, 2005: Message edited by: Bear Bibeault ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are different schools of thought when it comes to an issue like this.
Put logic in the JSP Verses write HTML in java.

For select boxes, I think writing a small amount of HTML in the java class is the lesser of the two evils. I would create a method that loops through the array list and builds the HTML Option tags (with the "selected" attribute in the appropriate places) and returns it as a string. This way there is no need to have looping and branching code in your JSP. Your web designers will see something like:

or with EL:


Others would rather sacrifice their first child than write a line of HTML in a Javabean. For those people there are some nice JSTL tags that allow you to iterate through a collection, and branch to dynamically add the "selected" attribute.

It certainly wouldn't hurt for your bean to expose methods for both approaches.
[ April 16, 2005: Message edited by: Ben Souther ]
 
Bhavik Patel
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ben Souther:
There are different schools of thought when it comes to an issue like this.
Put logic in the JSP Verses write HTML in java.

For select boxes, I think writing a small amount of HTML in the java class is the lesser of the two evils. I would create a method that loops through the array list and builds the HTML Option tags (with the "selected" attribute in the appropriate places) and returns it as a string. This way there is no need to have looping and branching code in your JSP. Your web designers will see something like:

or with EL:


Others would rather sacrifice their first child than write a line of HTML in a Javabean. For those people there are some nice JSTL tags that allow you to iterate through a collection, and branch to dynamically add the "selected" attribute.

It certainly wouldn't hurt for your bean to expose methods for both approaches.

[ April 16, 2005: Message edited by: Ben Souther ]


Thanks ...but i didn't get you for


how i will traverse the ArrayList and put the values dynamially in Option tag.....Is this be done in scriplet or in Java class...I dont know how to access html tags in Java class
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You traverse the ArrayList in your bean and return the results to the JSP.


When you do this, you're pulling all of the functional code out of the JSP, leaving only one getProperty tag for the HTML designers to break.


[ April 16, 2005: Message edited by: Ben Souther ]
 
Bhavik Patel
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks ben ...that works fine
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I left out, and you'll want to consider is code to handle nulls.
I would normally check to see if the ArrayList is null before trying to get an iterator from it and just return an empty string if it is.

Depending on how much you trust your data, you may also want to check for null values coming out of the iterator.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!