Saturday, August 20, 2011

Spring MVC table data mapping with spring form bean in multiaction controller using JSTL

This is an example for mapping the table data values with spring form bean

BO classes:

class Parent{
private String parentName;
private List child1Lst =
LazyList.decorate(
new ArrayList(),
FactoryUtils.instantiateFactory(Child1.class));

//setter and getter methods for above variables
}

class Child1{
private String child1Name;
private String child1Age;
private List child2Lst =
LazyList.decorate(
new ArrayList(),
FactoryUtils.instantiateFactory(Child2.class));

//setter and getter methods
}

class Child2{
private String child2Name;
private String child2Age;
//setter and getter methods
}

LazyList used to handle the null Pointer exception when bo intialized. LazyList and FactoryUtils from org.apache.commons.collections

Controller Methods

@RequestMapping("/serviceBundleInfo.edo")
public ModelAndView getTableData(@ModelAttribute("parent") Parent parent){
Child1 c1=new Child1();
c1.setChild1Name("name1");
c1.setChild1Age("10");

Child2 c2 = new Child2();
c2.setChild2Name("name2");
c2.setChild2Age("11");

List child2Lst=new ArrayList();
child2Lst.add(c2);

c1.setChild2Lst(child2Lst);
List child1Lst=new ArrayList();
child1Lst.add(c1);

parent.setchild1Lst(child1Lst);

return new ModelAndView("tabledata", "parentBO", parent);
}

@RequestMapping("/processsubmit.do")
public ModelAndView submittedData(@ModelAttribute("parent") Parent parent,HttpServletRequest request, HttpServletResponse response){
System.out.println("Changed Data: "+parent.getParentName());
for(Child1 child1:parent.getChild1Lst){
System.out.println("Changed Data: "+child1.getChild1Name());
System.out.println("Changed Data: "+child1.getChild1Age());
for(Child2 child2:child1.getChild2Lst){
System.out.println("Changed Data: "+child1.getChild2Name());
System.out.println("Changed Data: "+child1.getChild2Age());
}
}
}



jsp:

tabledata JSP

<?xml:namespace prefix = form />
<form:form action="processsubmit.do" modelattribute="parent">
<form:input path="parent.parentName">

--HTML table format

<tr>

<td>
child1 Name
</td>

<td>
child1 Age
</td>

<td>
child2 Name
</td>

<td>
child2 Age
</td>
</tr>

<?xml:namespace prefix = c />
<c:foreach items="${parent.child1Lst}" var="child1Lst" varstatus="featurerowvalue">
<c:foreach items="child1Lst[${featurerowvalue.index}].child2Lst}" var="child2Lst" varstatus="featuredetailRow">

<tr>

<td>
<form:input path="child1Lst[${featurerowvalue.index}].child1Name">
</td>

<td>
<form:input path="child1Lst[${featurerowvalue.index}].child1Age">
</td>

<td>
<form:input path="child1Lst[${featurerowvalue.index}].child2Lst[${featuredetailRow.index}].child2Name">
</td>

<td>
<form:input path="child1Lst[${featurerowvalue.index}].child2Lst[${featuredetailRow.index}].child2Age">
</td>
</tr>
</c:foreach>
</c:foreach>
</tbody></table>
<input id="submitBtn" value="Submit" type="submit">
</form:form>

Click on Submit button, new entered data will be get in processsubmit method.

Thanks
kssrao