struts中操作一个集合数据

这几天一直被一个问题给困住了,总是调试不过去,以前好像也考虑这个问题。现在终于让我解决了,所以,一定要把这个东西给记录下来。
就是我们在开发过程中,经常可能遇到要修改多条记录的情况,这个时候当然我们可以用原来的办法,用request.getParameter()来一条一条记录的修改,但是,现在使用struts框架,这个问题,解决起来就方便了许多。
注意1.checkbox在提交的时候,只会将那些被选中了的即obj.checked=true的那些值提交,而为false的值不会提交。
注意2.使用struts框架里卖的ActionForm和标签来解决这个问题。例如:
ActionForm的代码:
public class TransSlaitemGridForm extends WafForm {

/**
* An array of VoTransSlaitem.
*/
private VoTransSlaitem[] transSlaitems = null;
........
类VoTransSlaitem的代码:
public class VoTransSlaitem implements Serializable {
private long transSlaId =0;
private int custSlaitemId =0;
.......
这个ActionForm里面有一个数组属性。在页面将对这个数组多条属性里面的数据进行处理。则我们在页面上的各属性应该这样定义。
<input type="hidden" name="transSlaitems[<%=rowIndex %>].transSlaId" value="<bean:write name="transSlaitem" property="transSlaId"/>"/>
这样的话提交,就会将这个属性的值赋给ActionForm里的属性transSlaitems[index].transSlaId,这样就可以修改多条记录了。
但是,注意在AcitonForm里需要在reset方面对属性tansSlaitems构造数组对象。
如:
public void reset(ActionMapping mapping, HttpServletRequest request) {
int selectedTransSlaitem = 0;
if (request.getParameter("selectedTransSlaitem") != null)
{
selectedTransSlaitem=Integer.parseInt(request.getParameter("selectedTransSlaitem"));
transSlaitems = new VoTransSlaitem[selectedTransSlaitem];
for(int i=0;i<selectedTransSlaitem;i++)
transSlaitems[i]= new VoTransSlaitem();
}
}
这样才不会出错。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章