wood burning stoves
The moose likes Struts and the fly likes Problem with html:options Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Problem with html:options" Watch "Problem with html:options" New topic

Problem with html:options

Raj Chukka

Joined: Apr 09, 2007
Posts: 22
iam able to populate the collection values in JSP but not able to retrieve in Action class. iam pasting the code below. Can someone help me whats wrong with the code here.

1. JSP.

<%@ page language="java"%>
<%@ page import="java.util.ArrayList,strutsexamples.Vendor" %>

<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<title>Test html:multibox Tag</title>


ArrayList vendors = new ArrayList();

vendors.add(new Vendor("V1", "Vendor 1"));
vendors.add(new Vendor("V2", "Vendor 2"));
vendors.add(new Vendor("V3", "Vendor 3"));
vendors.add(new Vendor("V4", "Vendor 4"));
vendors.add(new Vendor("V5", "Vendor 5"));
vendors.add(new Vendor("V6", "Vendor 6"));
vendors.add(new Vendor("V7", "Vendor 7"));
vendors.add(new Vendor("V8", "Vendor 8"));
vendors.add(new Vendor("V9", "Vendor 9"));
vendors.add(new Vendor("V10", "Vendor 10"));
pageContext.setAttribute("vendors", vendors);


<body bgcolor="white">

<html:form action="example.do">

<table border="0" width="100%" align="left">

<th align="right">Multiple Select From A Collection (Using <html ptions> :</th>
<td align="left">
<html:select property="vendorList" size="10" multiple="true">
<html ptions collection="vendors" property="vendorNumber" labelProperty="vendorName"/>
<td> </td>
<td><html:submit value="submit"/></td>

2. Vendor.java

package strutsexamples;

public class Vendor

private String vendorNumber;
private String vendorName;

public void setVendorNumber(String number) {
this.vendorNumber = number;
public void setVendorName(String name) {
this.vendorName = name;
public String getVendorNumber() {
return vendorNumber;

public String getVendorName() {
return vendorName;

public Vendor (String vendorNumber, String vendorName) {
this.vendorNumber = vendorNumber;
this.vendorName = vendorName;


3. Action class

package strutsexamples;

import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class TestAction extends Action {

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

TestFormBean testFormBean = (TestFormBean)form;

System.out.println("action 1");
ArrayList vendors = testFormBean.getVendorList();

for (int i=0; i < vendors.size(); i++) {
Vendor vendor = (Vendor) vendors.get(i);
System.out.println("Vendor Number = "+ vendor.getVendorNumber());
System.out.println("Vendor Name = "+vendor.getVendorName());
return mapping.findForward("success");


5. Form Bean

package strutsexamples;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

import java.util.ArrayList;

public class TestFormBean extends ActionForm {

private ArrayList vendorList = new ArrayList();

public ArrayList getVendorList() {
System.out.println("form bean 1");
System.out.println("vendor list = " + vendorList);
return (this.vendorList);

public void setVendorList(ArrayList vendorList) {
this.vendorList = vendorList;

iam able to populate the values in <html:select> tag. But once after selecting the values and click the submit button iam getting error. The values are not populated into "Form Bean". iam expecting an array list of vendors to be populated into Form bean array list.

Thanks in advance.

Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33124


We're pleased to have you here with us here at JavaRanch, but there are a few rules that need to be followed, and one is that proper names are required. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character.

Forum Sheriff
[ March 23, 2008: Message edited by: Jeanne Boyarsky ]

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
First of all, it's important to understand that anything that comes from an HTML form when it's submitted is a String or an array of Strings. HTML knows nothing about any other Java object types such as ArrayLists.

When Struts populates an ActionForm, it is using data from the HTML form as it was submitted, so the only data that it can accept as parameters to the setters on your Action form is either a String or an array of Strings. There are some exeptions to this rule. If your setter has an int parameter, for example, Struts will try to convert it. However, an ArrayList is not one of those exceptions, and is never allowed as a parameter to an ActionForm setter method that will be called by Struts.

So, in your example, the only way Struts will properly call the setVendorList method is if it accepts a String array as a parameter, not an ArrayList. That string array will consist of only the vendor numbers for each vendor selected, since that's what you specified as the property attribute.

One more word about best practices: It's generally not a good idea to build a list such as your list of vendors in the JSP. In a real situation, you would likely be getting the list from a DataBase, and in a Model/View/Controller architecture, a view object such as a JSP should not be getting data from a database.

I'd suggest you create a preparatory action for this JSP that populates the list of vendors, places it either in the ActionForm as a property or by itself in request scope, and then forwards to the JSP.
[ March 23, 2008: Message edited by: Merrill Higginson ]

Consultant, Sima Solutions
Raj Chukka

Joined: Apr 09, 2007
Posts: 22
Thanks Merrill for the Reply.

I have converted the ArrayList to String[] and i was able to see multiple values selected.

I still have one more question. How do i retrieve "vendorName" in the above scenario along with "vendorNumber" as i need to populate the Vendor object and pass it back to business tier.

Raj Chukka
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864

Don't forget to change your display name... It's important.

regarding your question:

The only thing the HTML form is going to send is the Vendor number, not the vendor name. There's no way around that. However, you can structure your application so that you can look up the complete vendor object given the vendor number. In a database application, you can just query the database for a given vendor number and get the rest of the vendor information. If you already have all the vendors in a list, you can look up the vendor in the list. When doing this type of lookup in Java, it's helpful to ovverride the object's equals() method.

In the example below, I've overriden the equals method such that two Vendors are considered equal if they both have the same vendor number. That way later on when I'm looking for a specific vendor number in a list of vendors, I can create a search object with only the vendor number populated and still have the statement vendorlist.indexOf(searchVendor) find the correct object.

Here's the example:

The new Vendor object:

The new PrepareVendor Action:

The entries in the struts-config.xml file:

The JSP:

The new TestAction. Note the use of vendorList.indexOf(searchVendor) that I talked about earlier. If I hadn't overridden the equals method of the Vendor class, this would not work.

You would start this application with the URL: http://localhost:8080/strutstest/prepareVendor.do

[ March 23, 2008: Message edited by: Merrill Higginson ]
[ March 23, 2008: Message edited by: Merrill Higginson ]
I agree. Here's the link: http://aspose.com/file-tools
subject: Problem with html:options
jQuery in Action, 3rd edition