0

In my JSP page I've displayed list of people with id, name and email fields as follows:

<s:iterator value="peopleList">
<tr>
    <td><input type="checkbox" name="checkedId"  value="<s:property value="id"/>" /></td>
    <td><s:property value="id"/></td>
    <td><s:property value="firstName"/></td>
    <td><s:property value="lastName"/></td>
    <td><s:property value="email"/></td>                        
</tr>
</s:iterator>

My action class is as follows:

    private String[] checkedId;
    private List<People> peopleList;
    PeopleDao peopleDao = new PeopleDaoImpl();
    People people = new People();
    private List<People> checkBoxList = new ArrayList<People>();

 public String checkBox(){      

     System.out.println("Hello");
        for(String p: checkedId){
        System.out.println(p);
        }
         return SUCCESS;
    }

Currently I'm sending an id as check-box value and printing them out in action class. This works fine but what I want is to send the whole checked row of id, names and email as an object of type People as check-box value to store it in checkBoxList. How do I achieve this?

I tried adding var="list" in the iterator and using the "list" as the value for check-box with name="checkBoxList" but it didn't work out.

Roman C
  • 49,761
  • 33
  • 66
  • 176
Sujal
  • 671
  • 1
  • 16
  • 34

2 Answers2

0

In your html, keep the values of all the other attributes as hidden input tags in a form so that when the user submits the form you can get all the attributes in the code.

<form id="peopleSelect" action="updateSelectedPpl.action">
<s:iterator value="peopleList">
<tr>
    <td><input type="checkbox" name="checkedId"  value="<s:property value="id"/>" /></td>
    <td><input type="hidden" name="firstNames"  value="<s:property value="firstName"/>" /><s:property value="firstName"/></td>
    <!-- do the same for all the others -->
    <td><s:property value="id"/></td>
    <td><s:property value="lastName"/></td>
    <td><s:property value="email"/></td>                        
</tr>
</s:iterator>
</form>

your action class code can be like this. make sure your action class implements ServletRequestAware

String[] checkedIds = request.getParameterValues("checkedId");
String[] firstNames = request.getParameterValues("firstnames");

for(int i=0; i<checkedIds.size; i++){
  //get all the other attributes and populate your pojo here.
  firstName = firstNames[i];

}
Arvind Sridharan
  • 3,885
  • 4
  • 29
  • 54
  • What I wanted to know was if it was possible to send an object instead of having to send its fields like ids, names separately – Sujal Dec 23 '13 at 08:28
  • yes, you can do that using ModelDriven. see this answer for more details http://stackoverflow.com/a/13031499/728610 – Arvind Sridharan Dec 23 '13 at 10:53
0

You can use indexed property names when submitting form data.

<s:iterator value="peopleList" status="st">
<tr>
    <td><s:checkbox name="checkBoxList[%{#st.index}].checkedId"  value="%{id}" /></td>
    <td><s:textfield name="checkBoxList[%{#st.index}].id"/></td>
    <td><s:property value="checkBoxList[%{#st.index}].firstName"/></td>
    <td><s:property value="checkBoxList[%{#st.index}].lastName"/></td>
    <td><s:property value="checkBoxList[%{#st.index}].email"/></td>                        
</tr>
</s:iterator> 
Roman C
  • 49,761
  • 33
  • 66
  • 176