File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Struts and the fly likes Struts: form with multiple text fields with same name Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Barcodes with iOS this week in the iOS forum
or Core Java for the Impatient in the Java 8 forum!

JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts: form with multiple text fields with same name" Watch "Struts: form with multiple text fields with same name" New topic

Struts: form with multiple text fields with same name

Joe Olliges

Joined: Jun 21, 2006
Posts: 1
I am trying to use <html:form> tags with multiple text fields with the same name and getting the following error when accessing the form (index.jsp):
javax.servlet.ServletException: Exception thrown by getter for property: "cableCost[0]" of bean: "org.apache.struts.taglib.html.BEAN"

Here is my ActionForm code:

import org.apache.struts.action.ActionForm;

public class TestForm extends ActionForm
* The unique id required by the serializable implementation.
private static final long serialVersionUID = 1L;
private String cableCost [];
* @return Returns the cableCost.
public String getCableCost(int index)
return cableCost[index];
* @param cableCost The cableCost to set.
public void setCableCost(int index,String cableCost)
this.cableCost[index] = cableCost;

Here is my Action Class:

public final class TestAction extends Action
public ActionForward execute(
ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
System.out.println("Start of test Action");
String cableCost1 = ((TestForm) form).getCableCost(0);
String cableCost2 = ((TestForm) form).getCableCost(1);
String cableCost3 = ((TestForm) form).getCableCost(2);

System.out.println("Cable 1" + cableCost1);
System.out.println("Cable 2" + cableCost2);
System.out.println("Cable 3" + cableCost3);

return mapping.findForward("success");

Here is my form:
<%@ taglib uri="/WEB-INF/tlds/struts-html.tld" prefix="html" %>
Test Application
<html:form action="/test" method="post">
Cable Cost 1<html:text property="cableCost[0]" size="16"/><br />
Cable Cost 2: <html:text property="cableCost[1]" size="16"/><br />
Cable Cost 3: <html:text property="cableCost[2]" size="16"/><br />

Thanks for the help in advance,
vidya sagar
Ranch Hand

Joined: Mar 02, 2005
Posts: 580
In the TestForm you change these two methods from


Hope now it works
vidya sagar
Ranch Hand

Joined: Mar 02, 2005
Posts: 580
and one more thing you need to change your TestAction code accordingly
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Using indexed getters and setters is perfectly fine. That's not the problem. Is your ActionForm in request scope? If so, that's the problem.

The easy solution would be to change the scope to session.

If you're using indexed properties for an ActionForm that is in request scope, you must someehow rebuild the cableCost array before Struts calls the indexed getters and setters. Otherwise, it will throw either a null pointed exception or an index out of bounds exception. You can do this either by overriding the reset() method in your form bean, or by using a lazy initialization technique in your getter method.

Consultant, Sima Solutions
I agree. Here's the link:
subject: Struts: form with multiple text fields with same name