• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Struts: form with multiple text fields with same name

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the TestForm you change these two methods from

To


Hope now it works
 
vidya sagar
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and one more thing you need to change your TestAction code accordingly
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic