4

I am working on a project where I have to send data from JSP page to action class in the form of List, I am setting List<Object> from action class first time when it mapped with JSP page, when I am doing action for saving the details, In action I get a values of List<Object> like as Cartesian Product for each value.

e.g. If there are List of 3 Objects(having 4 member fields) passing to JSP, when returning to action it generates 12 objects with different values of JSP.

The Image shows the Demo UI, it is not a table, but each Row represents an object of list

The image shows the Demo UI which I need
In The action class

 List<Pojo> PojoList = new ArrayList<Pojo>();

Can anyone suggest me what approach should I use, this is new scenario for me, I also tried some example from internet but not succeed, I also iterate the list in JSP but It was giving me an error.(not exactly error but it does not give fields on JSP);

In JSP:

<c:forEach var="pojo" items="${pojoList}">
    <s:textarea name="pojo.field1">
    <s:textarea name="pojo.field2">
    <s:textarea name="pojo.field3">
    <s:textarea name="pojo.field4">
</c:forEach>

Return In Action Class method(It prints 12 objects)

try {
    System.out.println("List : "+pojoList);
    for (Iterator<Pojo> iterator = pojoList.iterator(); iterator.hasNext();) {
        Pojo pojo = (Pojo) iterator.next();
        System.out.println("\n MB : "+pojo);
       }
} catch (Exception e) {
    e.printStackTrace();
}

Please suggest me whats going wrong.

Roman C
  • 49,761
  • 33
  • 66
  • 176
MegaBytes
  • 6,355
  • 2
  • 19
  • 36

1 Answers1

1

If you have 3 pojos in the list then each one have 4 fields added to the list by default, so 3*4 = 12. The code is working fine, but if you change it like

<s:iterator value="pojoList" status="stat">
    <s:textarea name="pojoList[%{#stat.index}].field1">
    <s:textarea name="pojoList[%{#stat.index}].field2">
    <s:textarea name="pojoList[%{#stat.index}].field3">
    <s:textarea name="pojoList[%{#stat.index}].field4">
</s:iterator>

it will use the same index for each field, so 3 pojos will be created/updated.

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • I did this way but it returns a list as null, – MegaBytes Oct 15 '14 at 18:16
  • This is because you are missing getters and setters for the beans. – Roman C Oct 15 '14 at 18:33
  • Hi Roman, I have already created the getter & setter for the fields and the list object – MegaBytes Oct 16 '14 at 05:52
  • Then you should configure the action to include `params` interceptor. – Roman C Oct 16 '14 at 09:30
  • 2
    Hi Roman, My Problem is I have to pass the List from action class to JSP, the list size is not fixed, Also I have to get the list return to the Action class. The list size may increase in JSP page, I couldn't understand what I have to do? As I am fresher in struts2 so I am not aware how to do? Please suggest what steps or approach I use? – MegaBytes Oct 17 '14 at 09:59