[Opensource] Using the html:select tag
Peter A. J. Pilgrim
peterp at xenonsoft.demon.co.uk
Wed Aug 14 16:47:55 PDT 2002
Wolf Thompson wrote:
> There has to be a reasonably simple answer to this one ? I just haven?t
> found it.
>
>
>
> I want to create a dropdown selection via the html:select tag. The
> contents of the dropdown box will be dynamically generated from one
> field of a searchAndRetrieveList ? i.e., names of cities stored in a
> database table.
>
>
>
> Even a **clue** as to how I can accomplish this would be greatly
> appreciated.
>
>
>
> So far, every Input I?ve used with the html:select tag has given me a
> different Controller error ? either it doesn?t have valid values or I?m
> getting a cast exception.
>
>
>
> Thanks in advance,
>
> Wolf Thompson
>
You need to use the LabelValueBean in Struts. I think you can get it
from the Struts Example in the value 1.0.2. In fact it is a very
simple JavaBean.
class LabelValueBean {
String label, value;
...
public String getLabel();
public String getValue();
}
You create a Java Collection with LabelValueBean object as element.
Each `LabelValueBean' is set up with label and the actual value
you want to see in a your down menu.
List el = new java.util.ArrayList();
el.add(
new LabelValueBean(
"Nelson Mandela", "nmandel" ));
...
Once you created a Java Collection put in one of JSP scopes
and then forward to JSP view.
request.setAttribute( AppGlobal.EMPLOYEE_NAME_LIST, el );
Or if you have an Struts ActionForm class you can store the
menu list as an attribute. For example
class EmployeeEntryActionForm {
List employeeNameList = new java.util.ArrayList();
public List getEmployeeNameValues() ;
...
}
Then in a JSP you need to define a page attribute to refer the
Java Collection inside the action form
<bean:define id="employeeNameValues"
name="employeeEntryForm"
property="employeeNameValue"
type="java.util.List" />
You need to embed <html:options> inside the <html:select>. Admitted
the java doc for HTML Options is really confusing and it take me
a long time to figure it out 12 months ago.
<html:select property="employeeName" size="1" >
<html:options collection="employeeNameValues"
property="value" labelProperty="label" />
</html:select>
HTHEWNI -- Hope This Helps Everybody Who Needs It
--
Peter Pilgrim +-----\ +-++----++----+
Java Technologist | | | || || | 'n' Shine
| O | | || --+| ---+
/\ | ._ / | | \ \ | |
/ \ | | \ \ | |+-- || ---+ A new day
/_ _\ "Up" | | | | | || || | is coming
|| +-+ +-+ +-++----++----+
<home page="http://www.xenonsoft.demon.co.uk/" />
More information about the Opensource
mailing list