wood burning stoves 2.0*
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 Murach's Java Servlets and JSP this week in the Servlets 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
Author

Struts: form with multiple text fields with same name

Joe Olliges
Greenhorn

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" %>
<html:html>
<head>
<title>
Test Application
</title>
</head>
<body>
<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 />
<html:submit>
Submit
</html:submit>
</html:form>
</body>
</html:html>

Thanks for the help in advance,
Ollig
vidya sagar
Ranch Hand

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

To


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.


Merrill
Consultant, Sima Solutions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Struts: form with multiple text fields with same name
 
Similar Threads
Struts: No getter method for property name problem
getting value of html texts created by iterating
Session problem in Struts and Tomcat
Array inside Struts ActionForm
form values not showing in jsp