---
title: "Struts接收表单提交的多行数据"
author: "gu6866"
date: "2012-04-28T13:33:26Z"
tags: []
categories: ["开源软件"]
slug: "struts"
draft: false
lastmod: "2012-04-28T13:55:30Z"
id: 1503322
url: "https://premier9527.iteye.com/blog/1503322"
---

/**
 * 扩展ArrayList类：重写get方法用来接收多行数据时防止下标越界
 * @author Administrator
 *
 */
public class AutoArrayList extends ArrayList { 

       private Class itemClass;
       public AutoArrayList(Class itemClass){
    	   this.itemClass=itemClass;
       }
       
       public Object get(int index){
    	   try{
    		   while(index>=size()){
    		       add(itemClass.newInstance());
    		   }
    	   }catch(Exception e){}
    	   return super.get(index);
       }
}
------------------------------------------------------
/**
 *ActionForm:  接收多行TCompareField数据tcompareFieldList
 */
public class TCompareFieldListForm extends ActionForm{

    private List comparefields = new AutoArrayList(TCompareFieldForm.class);
    
    public List getComparefields(){
         return this.comparefields;
    }
    public void setComparefields(List comparefields){
        this.comparefields= comparefields;
    }
    
}
-----------------------------------------------------
/**
 * ActionForm:接收TCompareField数据
 */
public class TCompareFieldForm extends ActionForm implements java.io.Serializable
{

    // Fields    

    private Long id;
    private Long compareId;
    private String fieldDescribe;
    private Long fieldLength;
    private String fieldType;
    private Long dictionaryId;
    private String fieldName;
    private Long targetRowId;
    private String compareMode;
    private String isUnique;
    private String isSimpleOutput;
    private String isDetailOutput;
    private String isInputSource;
    private Long seq;
    private String secretKey;
    private Long createBy;
    private Date createTime;
    private Long lastUpdateUser;
    private Date lastUpdateTime;

    public Long getId()
    {
        return id;
    }
    //getter&setter......
}
-----------------------------------------------------

在action中获取JSP表单提交的数据：
        //获取表单提交数据
        TCompareFieldListForm listForm = (TCompareFieldListForm)form;
        List<TCompareFieldForm> comparefields = listForm.getComparefields();

-----------------------------------------------------
JSP表单：表单的数据是一个数据集合list，注意字段的命名使用数组的方式--comparefields.targetRowId ，数组名要和TCompareFieldListForm 中的变量名对应,数组的属性要和TCompareFieldForm中的变量对应

<form action="">
<%
                    for(int i=0;i<compareRelateCheckList.size();i++){ 
                    VCompareInputSourceCheck inputSource=compareRelateCheckList.get(i);
                 %>
                 <tr class="tableRow" onmouseover="this.style.background='#D6E8F8';" onmouseout="this.style.background='#ffffff'">
                   <td align="center" width="20%">
                          <input type="hidden" id="comparefields.targetRowId" name="comparefields.targetRowId" value="<%=inputSource.getTargetRowId()%>">
                          <input type="hidden" id="comparefields.fieldDescribe" name="comparefields.fieldDescribe" value="<%=inputSource.getFieldDescribe()%>">
                          <%=inputSource.getFieldDescribe()%>
                   </td>
  </tr>
                 <%}%>
</form>