Reputation: 6158
Using Struts 1.3.
Submitting forms as a collection using the <logic:iterate>
tag I'd like to format multiple records of user to edit values.
When the data is submitted back to the Action
how can it handle a list of form in an Action form? Is there a way to submit the results as a collection of forms?
For example, I have List<EmployeeForm>
and I am iterating these records on my jsp page and it's working fine. But the records are editable on the JSP page, so after modifying the records and pressing the submit button I need the List<EmployeeForm>
with updated records inside my action class to update the records inside the DB.
update, my jsp page is below given:
<html:form action="modify.do" styleId="LogicIterateForm" method="post">
<table style="font-weight:bold">
<tr><td>Employee ID</td><td>Employee Name</td></tr>
<logic:iterate id="employee" name="LogicIterateForm" property="emp" indexId="i">
<tr>
enter code here
</tr>
</logic:iterate>
<tr>
<html:submit onclick="submitForm()">Modify</html:submit>
</tr>
</table>
</html:form>
action: to perform on clicking of Modify
button
LogicIterateForm logicIterateForm=(LogicIterateForm)form;
List<Employee> empList=logicIterateForm.getEmp();
System.out.println("Size of emp:::::"+empList.size());
if(empList!=null && empList.size()>0)
{
for(Employee emp:empList)
{
if(emp!=null)
{
System.out.println("EmployeeID:::::::::::"+emp.getEmpId());
System.out.println("EmployeeName:::::::::::"+emp.getEmpName());
}
}
}
and it is working fine to send the list of employees inside the action but unfortunately i am unable to get the updated form fields value inside my action. please help me where i am doing mistake.
and below is my ActionForm
public class LogicIterateForm extends org.apache.struts.action.ActionForm {
private List<Employee> emp=new ArrayList<Employee>();
public List<Employee> getEmp() {
return emp;
}
public void setEmp(List<Employee> emp) {
this.emp = emp;
}
and Employee class is a plan java class with setter and getter of empId and empName
Upvotes: 0
Views: 11512
Reputation: 160181
The nutshell version is that the generated HTML will have an incorrect name
attribute. The ActionForm
list property is named emp
but you're calling it employee
.
The longer version includes some other miscellaneous stuff that should make things a little easier for you in the long run.
First, here's the Action
I used to display the form. Note that I am not putting anything into scope explicitly--the framework does this for us. It appears as though you're explicitly setting a form into scope for use by <logic:iterator>
, but that's redundant.
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
Employee emp1 = new Employee("1", "Dave");
Employee emp2 = new Employee("2", "Subodh");
((LogicIterateForm) form).setEmp(Arrays.asList(emp1, emp2));
return mapping.findForward("success");
}
Second, the JSP page can be made simpler because of the above. Note that the name of the input element must match the name in the form, emp
, otherwise Struts won't know what to do with the input value, and it will be ignored.
<logic:iterate name="empForm" property="emp" id="emp">
<tr>
<td><html:text name="emp" value="${emp.empId}" property="empId" indexed="true"/></td>
<td><html:text name="emp" value="${emp.empName}" property="empName" indexed="true"/></td>
</tr>
</logic:iterate>
Then inside the action that's being submitted to all the data will be in the form as expected.
List<Employee> emps = ((LogicIterateForm) form).getEmp();
for (Employee emp : emps) {
System.out.printf("%s: %s%n", emp.getEmpId(), emp.getEmpName());
}
That should do it.
Upvotes: 2