Struts: ActionForms with collections.

Discussion in 'Java' started by Brian Trapp, Apr 29, 2005.

  1. Brian Trapp

    Brian Trapp Guest

    I've just wasted a day or two trying to figure this one out. I have a
    ActionForm that contains a Collection of objects. I could iterate
    through them to display them (with indexed="true") just fine, but I
    would get this:


    javax.servlet.ServletException: BeanUtils.populate
    at
    org.apache.struts.util.RequestUtils.populate(RequestUtils.java:497)
    at
    org.apache.struts.action.RequestProcessor.processPopulate(RequestProcessor.java:798)

    When I tried to submit the generated form.

    I eventually solved the problem by fixing the List getter in the
    ActionForm as shown below.

    I hope this helps someone else searching for help on this...

    Brian Trapp

    ==============================================================================
    ActionForm code sample
    ==============================================================================

    package com.ibm.fishkill.baldy.fail.web;

    import java.util.ArrayList;
    import java.util.List;

    import org.apache.struts.action.ActionForm;

    public class FailLinkForm extends ActionForm {

    private static final long serialVersionUID = 3906083438741500976L;

    public static final String SESSIONKEY = "FailListForm";

    private String techId="";
    private String type="";
    private String sector="";
    private String level="";
    private String codes="";
    private List <PlyToProcessSet> plyLinks = new ArrayList
    <PlyToProcessSet>();

    public String getTechId() {
    return techId;
    }
    public void setTechId(String techId) {
    this.techId = techId;
    }
    public String getType() {
    return type;
    }
    public void setType(String type) {
    this.type = type;
    }
    public String getLevel() {
    return level;
    }
    public void setLevel(String level) {
    this.level = level;
    }
    public String getSector() {
    return sector;
    }
    public void setSector(String sector) {
    this.sector = sector;
    }
    public String getCodes() {
    return codes;
    }
    public void setCodes(String codes) {
    this.codes = codes;
    }
    public List <PlyToProcessSet> getPlyLinks() {
    return plyLinks;
    }
    public void setPlyLinks(List <PlyToProcessSet> plyLinks) {
    this.plyLinks = plyLinks;
    }
    public PlyToProcessSet getPlyToProcessSet(int index) {
    //This getter will fail when you submit the form if
    // you don't provide a mechanism to populate it on the fly
    while (index >= plyLinks.size()) {
    plyLinks.add(new PlyToProcessSet());
    }
    PlyToProcessSet ptps = plyLinks.get(index);
    return ptps;
    }
    public void setPlyToProcessSet(int index, PlyToProcessSet ptps) {
    System.err.println("GOT setter");
    }
    }

    ==============================================================================
    jsp code fragment
    ==============================================================================

    <html:form action="/actions/FailLinkAction" method="POST">
    <TABLE>
    <TR><TH>Ply Key</TH><TH>Ply Recipe</TH><TH>Process Link</TH></TR>
    <logic:iterate id="plyToProcessSet" name="FailLinkForm"
    property="plyLinks">
    <TR>
    <TH>
    <bean:write name="plyToProcessSet" property="plyKey"/>
    <html:hidden name="plyToProcessSet" property="plyKey" indexed="true"
    />
    </TH>
    <TD>
    <bean:write name="plyToProcessSet" property="plyRecipe"/>
    <html:hidden name="plyToProcessSet" property="plyRecipe"
    indexed="true" />
    </TD>
    <TD>
    <html:select name="plyToProcessSet" property="pdId" indexed="true">
    <bean:define id="psteps" name="plyToProcessSet"
    property="processSteps" type="java.util.Collection"/>
    <html:eek:ptions collection="psteps" property="key"
    labelProperty="value"/>
    </html:select>
    </TD>
    </TR>
    </logic:iterate>
    <TR><TD COLSPAN="3">Defect Codes: <html:text name="FailLinkForm"
    property="codes"/></TD></TR>
    <TR><TD COLSPAN="3"><html:submit property="action"><bean:message
    key="button.doLinkChange"/></html:submit></TD></TR>
    </TABLE>
    <html:hidden property="techId"/>
    <html:hidden property="type"/>
    <html:hidden property="sector"/>
    </html:form>
     
    Brian Trapp, Apr 29, 2005
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?ISO-8859-1?Q?Ra=FAl_Ant=F3n_Cuadrado?=

    struts and ActionForms

    =?ISO-8859-1?Q?Ra=FAl_Ant=F3n_Cuadrado?=, Sep 4, 2003, in forum: Java
    Replies:
    2
    Views:
    388
    =?ISO-8859-1?Q?Ra=FAl_Ant=F3n_Cuadrado?=
    Sep 5, 2003
  2. Doug Poland
    Replies:
    9
    Views:
    752
    VisionSet
    Sep 27, 2003
  3. gilgantic
    Replies:
    2
    Views:
    2,730
    Larry
    Feb 7, 2004
  4. Binoj A via JavaKB.com

    How create dynamic Actionforms in struts

    Binoj A via JavaKB.com, Aug 9, 2005, in forum: Java
    Replies:
    0
    Views:
    390
    Binoj A via JavaKB.com
    Aug 9, 2005
  5. Replies:
    3
    Views:
    465
    Tim Slattery
    Nov 20, 2007
Loading...

Share This Page