my dog learned polymorphism
The moose likes Struts and the fly likes html:select and html:options Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "html:select and html:options" Watch "html:select and html:options" New topic

html:select and html:options

Cory Max
Ranch Hand

Joined: Jul 20, 2005
Posts: 83
Hi Everyone,

My name is cory and I have a problem... no... its not alcoholism, not smoking, not drugs.... my problem is this darned html select control. Here is my scenario:

I have a class called AdminForm:

public class AdminForm extends ActionForm {
private ArrayList dealers;
private String currentDealer;

public void setDealers(ArrayList dealers) { this.dealers = dealers; }
public ArrayList getDealers() { return dealers; }
public void setCurrentDealer(String currentDealer) {this.currentDealer = currentDealer; }
public String getCurrentDealer() { return currentDealer; }

the ArrayList is a collection of dealers:

public class Dealer
public Dealer() { }
private String dealerNumber;
private String dealerName;
public void setDealerNumber(String dealerNumber)
{ this.dealerNumber = dealerNumber; }
public String getDealerNumber() { return dealerNumber; }
public void setDealerName(String dealerName)
{ this.dealerName = dealerName; }
public String getDealerName() { return dealerName; }

In my action class, I do something like the following:

AdminForm adminForm = new AdminForm();
ArrayList dealers = service.getVisibleDealers();

How do I output a html:select/html ptions box containing a list of the dealers? This wont work (below) Please help... this has taken me wayyyyyyy to long.

<pdk-struts-html:select name="ADMINFORM" property="currentDealer">
<pdk-struts-html ptions collection="dealers" property="dealerNumber" labelProperty="dealerName" />

This seems soooo simple... to have stumped me for sooo long.
[ January 28, 2006: Message edited by: Cory Max ]

There are only 10 types of people in this world... Those who understand binary and those who don't.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864

Believe me, you're not alone. There are a lot of posts on this forum dealing with html:select.

My best advice to you is to use the htmlptionsCollection instead of htmlptions. You can accomplish what you need to with either tag, but I find that the former is more intuitive and easier to use.

The documentation on both these tags can be found at:

Here's how you'd do it using htmlptionsCollection:

<html:select name="ADMINFORM" property="currentDealer">
<htmlptionsCollection property="dealers" value="dealerNumber" label="dealerName" />

What makes optionsCollection more intuitive is that the property is more like the property attribute for the other html:xxx tags. It's the property of a form bean that represents the collection of options.

With htmlptions, property means something entirely different: The property of the bean that will represent that "value" of each individual option. Collection represents the name of a bean that represents the collection of objects to be iterated over. Since your collection is actually a property of the form bean, using htmlptions requires a bean:define before the statement. Here's how you'd have to do it with htmlptions:

<bean:define id="dealersCollection" name="ADMINFORM" property="dealers" />
<html:select name="ADMINFORM" property="currentDealer">
<htmlptions collection="dealersCollection" property="dealerNumber" labelProperty="dealerName" />
[ January 29, 2006: Message edited by: Merrill Higginson ]

Consultant, Sima Solutions
Cory Max
Ranch Hand

Joined: Jul 20, 2005
Posts: 83
Wow... thanks. Your help is much appreciated.


D Sheth

Joined: May 22, 2006
Posts: 2

I have done the exactly what you provided. I think I am missing something though.

I am getting an error - [ServletException in:/jsp/searchBySystem.jsp] Cannot retrieve definition for form bean null'

Form is:
public final class SearchBySystemForm extends ActionForm {

private ArrayList systems;
private int systemId;

public int getSystemId() {
return systemId;

public ArrayList getSystems() {
return systems;

public void setSystemId(int i) {
systemId = i;

public void setSystems(ArrayList list) {
systems = list;


My Bean is:
public class System implements {
private int systemId;
private String systemName;
* @return
public int getSystemId() {
return systemId;

* @return
public String getSystemName() {
return systemName;

* @param i
public void setSystemId(int i) {
systemId = i;

* @param string
public void setSystemName(String string) {
systemName = string;


My Action Class is:
public class SearchBySystemAction extends Action {

public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {

String forward = "error";

ArrayList systemList = new ArrayList();
SearchBySystemForm systemForm;
systemForm = new SearchBySystemForm();

System system = new System();

system = new System();


forward = "showSystemList";

return (mapping.findForward(forward));


My jsp is:
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>

<title>System List</title>
<html:form action="/searchBySystem">
<html:select name="SYSTEMFORM" property="systemId">
<html ptionsCollection property="systemList" value="systemId" label="systemName"/>

Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Change property="systemList" to property="systems"
[ May 22, 2006: Message edited by: Merrill Higginson ]
Fidel Edwards
Ranch Hand

Joined: Mar 19, 2008
Posts: 234
I know it is bad thing to write on old posts.. But I really could not stop to give big Thanks to Merrill for such a useful post... which will always remain helpful..!

God Gave Me Nothing I Wanted, He Gave Me Everything I Needed.
I agree. Here's the link:
subject: html:select and html:options
jQuery in Action, 3rd edition