页面 表单有家庭信息,有成员信息,但是成员信息有多行(就是有多个人),一个人占一个table 的一行。
工程里面有成员的类。我这个actionfrom 怎么写? 我是这样想想的 这个actionfrom 里面定义 一个list ,这个list 里面放成员的类。
但是页面 怎么写  才能通过form 直接把数据 传递过去?这个list 的getter setter 方法要不要特别的写下?

解决方案 »

  1.   

    页面可以用javascript连接起来直接在form中弄一个字段接受就可以了之间用特殊符号隔开 比如| 或者,分开。
      

  2.   


    class People{
    private String name;
    private int age;//setter getter
    }class XXXX extends ActionForm{private List people=new ArrayList(); //setter getter
    }<form>
    <input name="people[0].name"> <input name="people[0].age"><br>
    <input name="people[1].name"> <input name="people[1].age"><br>
    <input name="people[2].name"> <input name="people[2].age"><br>
    .....
    </form>
      

  3.   

    将list传过去就行了。
    还有一种方法就是遍历整个form
      

  4.   

    终于帮你找到了。
    我之前用servlet做的。
    我想用struts应该也可以实现。你大概看下页面和servlet看是否能符合你的要求。addBooks.jsp <%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>My JSP 'addBooks.jsp' starting page</title>
        
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
    <script type="text/javascript">
    var index=1;
    function v(){

    var names = document.bookForm.name;
        var str="";
      for(var i=0; i< names.length; i++){
    if(names[i].value == "")
      str+="第 "+i+"行书名为空!\n";
    }
    if(str!=null)
    {
             alert(str);
             return false;
             }
             return true;
    }
    function addBook(){
    var tab1 = document.getElementById("tab1");//获得一个已有的table
    var tr = document.createElement("tr"); //为table创建一个tr
    tr.appendChild(createInput("书名:", "name"));//为tr创建需要的td,"书名"为文本text的值
    //name 为属性name文本框的名称
    tr.appendChild(createInput("作者:", "author"));
    tr.appendChild(createSelect());//创建一个下拉列表
    tr.appendChild(createRadio2());
    tr.appendChild(createInput("出版日期:", "publishDate"));
    tr.appendChild(createDelete(tab1));//创建一个删除按钮

    var tbody = document.createElement("tbody");//创建一个表体
    tbody.appendChild(tr);//把已经创建好的tr赋值给自己
    tab1.appendChild(tbody);
    }
    function createSelect(){
    var td2 = document.createElement("td");
    var text = document.createTextNode("价格:");
    td2.appendChild(text);
    var selPrice = document.getElementById("selPrice");//获取到下拉列表的id
    var newNode = selPrice.cloneNode(true);//复制一个节点参数true false 表示是否复制文本中的值。这里指是否复制select 中的值
    td2.appendChild(newNode); //把复制的值添加到新的一列中
    return td2; // 返回td
    }
    function createRadio()
    {
        index+=1;
        alert(index);
     var td=document.createElement("td");
    var rad=document.getElementById("rad");
    var rad2=document.getElementById("rad2");
    var newNode=rad.cloneNode(true);
    //var index=td.parentElement.rowIndex;
    newNode.name = "sex"+index;
    /// newNode.value="0";
    var newNode2=rad2.cloneNode(true);
    newNode2.name = "sex"+index;
    ///newNode.value='1';
    td.appendChild(newNode);
    td.appendChild(document.createTextNode("男"));
    td.appendChild(newNode2);
    td.appendChild(document.createTextNode("女"));
    return td;   
    }
    function createRadio2()
    {
     index+=1;
        alert(index);
        var td=document.createElement("td");
        var radio=document.createElement("input");
        var radio2=document.createElement("input");
        radio.type="radio";
        radio.value="0";
         radio.name="sex"+index;
         radio2.type="radio";
        radio2.value="1";
        radio2.name="sex"+index;
        td.appendChild(radio);
        td.appendChild(document.createTextNode("男"));
         td.appendChild(radio2);
         td.appendChild(document.createTextNode("女"));
        return td;
    }

    function createDelete(tab1){//创建一个删除按钮

    var td = document.createElement("td");//创建一个新的列

    var button = document.createElement("input");//创建一个input标签 
    button.type = "button";//标签的type=button
    button.value = "删除"; //value=删除
    button.onclick = function (){//标签的点击事件
    var row = this.parentElement.parentElement.rowIndex;//获得这一行的索引 从0开始
    tab1.deleteRow(row);//删除掉这一行
    };
    td.appendChild(button);//把这个button放入td中

    return td;
    }
    function createInput(bookText, bookName){//需要俩个参数 文本和名称

    var td = document.createElement("td");
    var text = document.createTextNode(bookText);//创建一个文本节点
    td.appendChild(text);
    var input = document.createElement("input");//创建一个input
    input.type = "text";//input type=text
    input.size = 10;//size=0
    input.name = bookName;//name=传进来的参数
    td.appendChild(input);//把文本框付给td

    return td;
    }
    </script>

      </head>
      
      <body>
       <table width="100%" border="0" cellspacing="0" cellpadding="4">

    <tr> 

    <td bgcolor="#000099"> 

    <table width="100%" border="0" cellspacing="0" cellpadding="4">

    <tr> 

    <td bgcolor="#FFFFFF">&nbsp;<b>*</b>&nbsp;</td>

    <td width="100%"><font color="#CCCCCC">&nbsp; <font color="#FFFFFF">增加书本</font></font></td>

    </tr>

    </table></td>

    </tr>

    <tr> 

    <td width="100%" bgcolor="#EAEAEA" colspan="2"> 

    <form name="bookForm" method="post"
    action="servlet/bookServlet?action=addBooks" >

    <table id="tab1">

    <tr>
    <td>书名:<input type="text" name="name" size="10"></td>
    <td>作者:<input type="text" name="author" size="10"></td>
    <td>价格:
    <select name="price" id="selPrice">
    <option value="0">请选择价格..</option>
    <option value=10>10</option>
    <option value=20>20</option>
    <option value=30>30</option>
    <option value=35>35</option>
    </select>
    </td>
    <td><input type="radio" name="sex" value="0" id="rad">男
    <input type="radio" name="sex" value="1" id="rad2">女
         </td>
    <td>出版日期:<input type="text" name="publishDate" size="10"></td>
    <td>
    <input type="button" value="增加"
    onclick="addBook();">
    </td>
    </tr>
    <input type="submit" name="aa" value="tijiao"/>
    </table>


    </form>

    </td>

      </tr>

    </table>
      </body>
    </html>servlet添加方法 public void addBooks() 
            throws ServletException, IOException{

    String[] name=request.getParameterValues("name");
    String[] author=request.getParameterValues("author");
    String[] price=request.getParameterValues("price");
    String[] publishDate=request.getParameterValues("publishDate");
     Book boo=null;

    for(int i=0;i<name.length;i++)
    {     boo=new Book();
         Users users=(Users)new PublicDAO().findByID(1);
       System.out.println(users.getId());
             boo.setUsers(users);
             PublicDAO a= new PublicDAO();
      boo.setName(name[i]);
         boo.setAuthor(author[i]);
         boo.setPrice(Double.parseDouble(price[i]));
         boo.setPublishDate(java.sql.Date.valueOf(publishDate[i]));
         a.save(boo);
        //Myentity.addbook(new Object []{name[i],price[i],author[i],publishDate[i],null});
    }

        request.getRequestDispatcher("/servlet/bookServlet?action=ViewBook").forward(request, response);

       

    }希望符合楼主的意思。
      

  5.   

    Map backed ActionForm你去搜索下这个,保证是你想要的,我也是刚使用了这个比如页面上的表单<input name="item(test1)"/>
    <input name="item(test2)"/>
    <input name="item(test3)"/>你只需要在普通的form里面增加一个HashMap item.
    之后你就可以从form里面取出item.再遍历item取出所有之前你配置的值.
      

  6.   

    我认为用josn字符串 应该不错
      

  7.   

    3楼的 方法 是我想的那样,但是不行 报错。
    java.lang.IndexOutOfBoundsException