• Post Reply Bookmark Topic Watch Topic
  • New Topic

Populating drop-down box in JSP using bean

 
Dilip kumar
Ranch Hand
Posts: 360
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a drop-down box in my JSP page and need to populate this control by the values returned
by method defined in bean class. I'm not sure about the syntax in JSP side. I really appreciate any suggestions.
Thanks

package business;
public class Common {
private String[] items = { "09/09/2002", "09/10/2002" , "09/11/2002" };

public String[] getPeriodEnding() {
return (items);
}
}
 
Dan Collins
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would recommend using a custom tag for your select/drop down box. Then you can pass the values from your bean to the custom tag. The custom tag will have the logic to loop through the items array and create the HTML for the select.
 
Sandeep. Oza
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dilipkumar,
foll. is the sample code of the jsp file:
<%
String dt[] = common.getPeriodEnding();
if (dt.length!=0) {
%>
<select name="Period">
<%
for(int i=0;i<dt.length;i++) {
%>
<option value="<%=dt[i]%>"> <%=dt[i]%> </option>
<%
}//end of for loop
%>
</select>
<%
} else {
%>

No Periods
<%
}//end of if-else
%>

Try.. It's simple...
Good DAy.
Sandeep Oza
[ September 11, 2002: Message edited by: Sandeep. Oza ]
 
Simon Brown
sharp shooter, and author
Ranch Hand
Posts: 1913
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I second the idea of using a custom tag for this type of logic. For an example of how a generic tag can be implemented to generate a drop-down list from a collection of arbitrary JavaBeans based upon their properties, check out Professional JSP Tag Libraries, Chapter 8 : Tag Patterns that can be downloaded in PDF format for free.
All the best
Simon
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!