You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Muthu <s_...@mumbai.tcs.co.in> on 2003/09/22 14:41:08 UTC
Help Regarding Dynamic AddRow
here is the sample program that i have done for dynamic addrow. but when i submit the form the action form gets resetted and i am not able to retrieve the rows that i have dynamically added. please help me.
Thanks in advance
<?xml version = '1.0' encoding = 'windows-1252'?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<form-beans>
<form-bean name="addbean" type="mypackage1.frmAddRowForm"/>
</form-beans>
<action-mappings>
<action path="/ActionMap" type="mypackage1.AddRowAction" name="addbean" scope="session">
<forward name="Success" path="/AddRow.jsp"/></action>
</action-mappings>
</struts-config>
//AddRowAction.java
package mypackage1;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import mypackage1.frmAddRowForm;
public class AddRowAction extends Action
{
/**
* This is the main action called from the Struts framework.
* @param mapping The ActionMapping used to select this instance.
* @param form The optional ActionForm bean for this request.
* @param request The HTTP Request we are processing.
* @param response The HTTP Response we are processing.
*/
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
frmAddRowForm af=(frmAddRowForm)form;
if(af.getaction().equals("Check")) {
String[] aid=af.getaId();
String[] adesc=af.getaDesc();
System.out.println("Total Rows="+af.gettxtRows());
for(int i=0;i<af.getaId().length;i++)
System.out.println(aid[i]+ "-$$$$$$-" +adesc[i]);
af.setRows();
System.out.println("Total Rows="+af.gettxtRows());
System.out.println(af.getlstBean().size());
af.settxtRows(new Integer( af.getlstBean().size()).toString());
return mapping.findForward("Success");
}
return mapping.findForward("Success1");
}
}
//frmAddRowForm.java
package mypackage1;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionMapping;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Collection;
import mypackage1.AccIdBean;
public class frmAddRowForm extends ActionForm
{
public frmAddRowForm()
{
lstBean=new ArrayList();
txtRows="0";
System.out.println("Add Row Form Constructor Called");
}
/**
* Reset all properties to their default values.
* @param mapping The ActionMapping used to select this instance.
* @param request The HTTP Request we are processing.
*/
public void reset(ActionMapping mapping, HttpServletRequest request)
{
super.reset(mapping, request);
}
/**
* Validate all properties to their default values.
* @param mapping The ActionMapping used to select this instance.
* @param request The HTTP Request we are processing.
* @return ActionErrors A list of all errors found.
*/
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request)
{
return super.validate(mapping, request);
}
public String[] getaId()
{
return this.aId;
}
public String[] getaDesc()
{
return this.aDesc;
}
public String getaction()
{
return this.action;
}
public String gettxtRows()
{
return this.txtRows;
}public String getcheck()
{
return this.check;
}
public ArrayList getlstBean()
{
return this.lstBean;
}
public void setaId(String[] str)
{
this.aId=str;
}
public void setaDesc(String[] str)
{
this.aDesc=str;
}
public void setaction(String act)
{
this.action=act;
}
public void settxtRows(String act)
{
this.txtRows=act;
}
public void setcheck(String act)
{
this.check=act;
}
public void setlstBean(ArrayList lstArr)
{
this.lstBean=lstArr;
}
public void setRows()
{
final int size = Integer.parseInt(this.txtRows);
if(lstBean.size()>0)
lstBean.clear();
for (int i = 0; i < size; i++) {
AccIdBean aBean=new AccIdBean(this.aId[i],this.aDesc[i]);
lstBean.add(aBean);
}
}
private String[] aId=null;
private String[] aDesc=null;
private String action="";
private String txtRows="";
private String check="0";
private ArrayList lstBean=null;
}