struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "James Childers" <>
Subject RE: Populating select boxes dynamically
Date Tue, 01 Jul 2003 13:21:24 GMT
> -----Original Message-----
> From: sriram []
> Sent: Tuesday, July 01, 2003 4:18 AM
> To: ''
> Subject: Populating select boxes dynamically
> I am fairly new to struts. I am using struts 1.0. I have 
> couple of list boxes in my input form which need to populated 
> with data retrieved from the database.
> What is the process to be followed? Can somebody give me 
> links to related examples on the net?

Hi Sriram,

This is pretty easy. You should read up on the Struts JavaDocs for LabelValueBean:

As well as for the <html:options> tag:

Since you're using 1.0 I *think* you'll need to grab the source for that bean and add it to
your project, but you'll want to do this anyway because it is extremely helpful (and pretty
simple insofar as beans go).

What you'll do at that point is add the elements which will be used in your select box in
a "pre" Action, which will add these LabelValueBeans to a Colletion, which will in turn be
added to the request.

Clear as mud? Maybe this will help:

1) Create a "pre" Action, which will prepare the data for the JSP which has the select box.

2) In this Action, do your database operations and store each result in a LabelValueBean.
It will look something like this:
	List aList = new ArrayList();
	while ( {
		LabelValueBean lvb = new LabelValueBean(rs.getString("labelCol"), rs.getString("valueCol"));
	// Now, add it to the request so we can see it in our JSP
	request.setAttribute("yerSelectBox", aList);

3) In your JSP, you'll just (hopefully!) need to reference the List you added to request scope
using the <html:options> tag:
	<html:select property="anActionFormElement">
		<html:options collection="yerSelectBox" property="value" labelProperty="label" />


-= James

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message