日期:2014-05-16  浏览次数:20340 次

jsp页面向form中提交list(批量提交)

???? 近段日子项目需要做一个向form里提交list的操作,从网上找了不少信息,虽然还是有些地方不理解,但终于还是将这个功能实现了。现在总结一下,有的地方我不了解标注了一下,也希望大虾给个解释,不胜感激。也希望能给需要的兄弟们一些信息。

???? 首先,我们需要的list的要继承java.util.ArrayList重新定义一个OurList, 并重写了public Object get(int index)接口,防止越界,这个类是关键之所在,切记!!!

public class OurList extends ArrayList {

 private static final long serialVersionUID = 1L;

 private Class itemClass;

 public OurList(Class itemClass) {  
  this.itemClass = itemClass;
 }

 public Object get(int index) {
  try {
   while (index >= size()) {
    add(itemClass.newInstance());
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
  return super.get(index);
 }

}

???? ?然后,就要在ActionForm中声明我们需要的list:

public class OurActionForm extends ActionForm{
        private List neededList = new OurList<needClass>();//needClass为指定数据对象
get();set();方法
}

?再就是在jsp页面中的显示:

1.对于html:text格式如下:
	<logic:iterate id="needed" name="OurActionForm" property="neededList" indexId="index">
<tr>
<td align="left" width=3%><html:checkbox name="needed" property="argument" indexed="true"/>//indexed属性不能忘掉,用它来逐一遍历list中的记录。
</td>	
2.对于html:hidden格式如下:
<logic:iterate id="needed" name="OurActionForm" property="neededList" indexId="index">
<html:hidden ame="needed" property="argument" indexed="true"/>

?

?

?? 这样我们就完成可以多行数据提交的ActionForm设计,这里还有点提醒,如果从浏览器端传过来的行索引是跳跃是的,如缺少中间行,而你不想要这些数据的话,且首尾行index相差很大的话,(这个地方不太懂)这种方式可能不太适合。这种多行提交形式比较适合对固定行的数据处理。