Struts: ActionForms with collections.

B

Brian Trapp

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>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top