如何将对象作为复选框值从选中的行发送到 Struts 2 操作



在我的JSP页面中,我显示了具有idnameemail字段的人员列表,如下所示:

<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>

我的操作类如下:

    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;
    }

目前,我正在发送一个id作为复选框值,并在操作类中打印出来。这工作正常,但我想要的是将整个选中的id行、name 和电子邮件作为 People 类型的对象作为复选框值发送,以将其存储在 checkBoxList 中。我如何实现这一点?

我尝试在迭代器中添加var="list"并使用"列表"作为带有name="checkBoxList"复选框的值,但没有成功。

在 html 中,将所有其他属性的值保留为表单中的隐藏输入标记,以便在用户提交表单时获取代码中的所有属性。

<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>

确保你的动作类实现了 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];
}

您可以在提交表单数据时使用索引属性名称。

<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> 

最新更新