Hi,
I have a row with text box and a add(submit) button. when a user clicks that button a row has to be added. i tried with <logic:iterate> but couldn't.can u suggest me. below is my coding which includes jsp,action, form and bean.
//person.jsp
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ page
language="java"
contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"
%>
<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<META name="GENERATOR" content="IBM WebSphere Studio">
<META http-equiv="Content-Style-Type" content="text/css">
<TITLE>customercontact.jsp</TITLE>
<TABLE border="0" height="380px" align="center">
<html:form action="/person" >
<DIV >
<TABLE id="mytable">
<TR><TD><html:submit onclick="self.location='person.do'" /></TD></TR
<logic:iterate id="person" name="personForm" property="personList" type="com.hcltech.demowar.beans.PersonBean" >
<TR><TD>
<html:text name="person" property="name" indexed="true"></html:text>
</td></tr>
</logic:iterate>
</TABLE>
</DIV>
</TD>
</TABLE>
</TD>
</TR>
</TABLE>
</html:form>
//PersonForm.java (Action Form)
package com.hcltech.demowar.forms;
import java.util.ArrayList;
import java.util.Vector;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import com.hcltech.demowar.beans.PersonBean;
public class PersonForm extends ActionForm {
private String name = null;
/**
* Get name
* @return String
*/
public String getName() {
return name;
}
/**
* Set name
* @param <code>String</code>
*/
public void setName(String n) {
this.name = n;
}
/*
public void reset(ActionMapping mapping, HttpServletRequest request) {
// Reset values are provided as samples only. Change as appropriate.
phoneno = null;
name = null;
}
*/
private ArrayList personList = new ArrayList();
public void reset(ActionMapping mapping, HttpServletRequest request){
//this.personList = new ArrayList();
// String num = request.getParameter("personListLength");
//String num ="5";
try {
/*if (num != null) {
int len = Integer.parseInt(num);
for (int i = 0; i < len; i++)
this.personList.add(new PersonBean());
}*/
//this.personList.add(new PersonBean());
}catch (NumberFormatException e) {
e.printStackTrace();
}
}
public PersonBean getPerson(int ndx) {
return (PersonBean) personList.get(ndx);
}
public void setPersonList(int ndx, PersonBean p) {
System.out.println("am setting index");
personList.set(ndx, p);
}
public ArrayList getPersonList(){
return this.personList;
}
public void setPersonList(ArrayList p)
{
System.out.println("am setting");
this.personList =p;
}
public ActionErrors validate(
ActionMapping mapping,
HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
// Validate the fields in your form, adding
// adding each error to this.errors as found, e.g.
// if ((field == null) || (field.length() == 0)) {
// errors.add("field", new org.apache.struts.action.ActionError("error.field.required"));
// }
return errors;
}
}
//PersonAction.java (Action Class)
package com.hcltech.demowar.actions;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.hcltech.demowar.beans.PersonBean;
import com.hcltech.demowar.forms.PersonForm;
public class PersonAction extends Action {
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
System.out.println("in person action");
ActionErrors errors = new ActionErrors();
ActionForward forward = new ActionForward();
if(form instanceof PersonForm)
{
PersonForm personForm = (PersonForm)form;
ArrayList personList= personForm.getPersonList();
System.out.println("personList = " + personList.size());
personList.add(new PersonBean());
System.out.println("personList = " + personList.size());
System.out.println("name = " + personForm.getPhoneno());
request.setAttribute("personList",personList);
}
return mapping.findForward("onSuccess");
//return (forward);
}
}
//PersonBean.java
package com.hcltech.demowar.beans;
import java.io.Serializable;
public class PersonBean implements Serializable {
String name ="test";
String phoneno;
public PersonBean() {
super();
}
public String getName() {
return name;
}
public void setName(String newName) {
this.name = newName;
}
public String getPhoneNo() {
return phoneno;
}
public void setPhoneNo(String newPhoneNo) {
this.phoneno = newPhoneNo;
}
}
regards,
Mohan Raj V