例如:
            
<table width="200" border="1">
  <tr>
    <td>姓名</td>
    <td>年龄</td>
    <td>操作</td>
  </tr>
  <tr>
    <td><input type="text" value="张三"/></td>
     <td><input type="text" value="17"/></td>
    <td><input type="button" value="+"/></td>
  </tr>
   <tr>
    <td><input type="text" value="李四"/></td>
    <td><input type="text" value="16"/></td>
    <td><input type="button" value="-"/></td>
  </tr>
  <tr>
    <td><input type="text" value="王五"/></td>
    <td><input type="text" value="15"/></td>
    <td><input type="button" value="-"/></td>
  </tr>
</table>
<input type="button" value="保存"/>一行对应一个对象,数据库里都有对应的Id,这里的姓名和年龄都可以修改,点击 后面的  + 号  可以新增,点击 - 号可删除
,最后保存是要把修改的内容 提交全部更新。
这块还有几天要上线了,有些理不清楚了,希望指点下我该怎么做。

解决方案 »

  1.   

        数据库里都有对应的Id? Id唯一的话 就用SQL语句更新数据库就好了,更新完了就刷新页面。不知道是不是这样子。我是刚学的。
      

  2.   

      一个对象有对应的Id,问题是这里一次性提交,不好区分  具体属性是对应的那个Id,还有有的是新增,有的是更新。
      

  3.   

    每个tr里面加一个用户id的隐藏域、然后name的属性设置成name+隐藏域id。。
    当后台取的时候根据id来取里面的内容js添加删除、应该不是很难!
    如果有问题可以加我Q260575364
      

  4.   

    新增和更新怎么还不能区分么?
    新增的id肯定为空,只有更新的才有id
      

  5.   

      我是在前面,设的一个隐藏域,用于存储 这一行的id,如果这一行没保存 id为空,如果已经是保存了的,就有它的id,就是不知道 这样整个表单提交之后,这行的id 与 它之后的属性都是对应正确的不? 后台  private String[] names; private String ids;
     页面一行: <input type="hidden" name="ids"/>  <input type="text" name="names" /> 
    这样提交后 ,后台 ids[i]与 names[i]能对应上不?
      

  6.   

    jsp支持数组,只要你form中相应input元素的name属性相同就行
      

  7.   

    我被打击的绝望了,最后用的是用数组实现的,获取一个个属性 ,让后循环的一个个封装成对象,效果是实现了,
    怎么就是觉得不可靠,而且一个对象的属性很多,就有很多数组,那样封装对象起来一个个set 看起来有些繁琐。
    想问问还有更好的办法没有。
      

  8.   

    我用Struts 1.x  的ActionForm做的  form:public String getContractInformation(String key){
    String obj = null;
    try {
    String[] functionId = spl(key);
    ContractInformation cifo = contractInformations.get(functionId[1]);
    Method putMethod = cifo.getClass().getMethod("get"+functionId[0]);
    obj =  putMethod.invoke(cifo).toString();
    } catch (Exception e) {
    e.printStackTrace();
    }

    return obj;
    }
    public void setContractInformation(String key,String value){

    Class[] argsClass = new Class[1];
    argsClass[0] = value.getClass();

    String [] str = new String[1];
    str[0] = value;

    String[] functionId = spl(key);
    ContractInformation cifo = contractInformations.get(functionId[1]);

    if(cifo==null){
    cifo = new ContractInformation();
    }
    try {
    Method putMethod = cifo.getClass().getMethod("set"+functionId[0],argsClass);
    putMethod.invoke(cifo, str);
    contractInformations.remove(functionId[1]);
    contractInformations.put(functionId[1],cifo);
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    public void setContractInformations(
    Map<String, ContractInformation> contractInformations) {
    this.contractInformations = contractInformations;
    }
    public Map<String, ContractInformation> getContractInformations() {
    return contractInformations;
    }
    jsp:<c:forEach var="cifo" items="${opertionForm.contractInformations}" varStatus="status">
    <html:hidden property="contractInformation(informationId_${cifo.key})"/>
    <tr>
    <td>
    <html:text property="contractInformation(informationNumbers_${cifo.key})" />
    </td>
    <td>
    <html:text property="contractInformation(productRange_${cifo.key})" />
    </td>
    <td>
    <html:text property="contractInformation(unit_${cifo.key})" />
    </td>
    <td>
    <html:text property="contractInformation(unitPrice_${cifo.key})"  styleId="a${status.index}" onblur="blurText(${status.index})"/>
    </td>
    <td>
    <html:text property="contractInformation(quantity_${cifo.key})"  styleId="b${status.index}" onblur="blurText(${status.index})"/>
    </td>
    <td>
    <input id="c${status.index}" name = "sumMoneyH" type ="text" value = "???"/>
    </td>
    <td>
    非临时数据
    </td>
    </tr>
    </c:forEach>
      

  9.   

    public String[] spl(String string){
    String str=string;
    str =str.replaceFirst(str.substring(0, 1),str.substring(0, 1).toUpperCase())  ; 
    String[] functionId = str.split("_");
    return functionId;
    }