真善美

勇于承担责任; 如果你做不到,要提前跟别人说明,不要等到事情发生后再解释。

  BlogJava :: 首页 ::  :: 联系 :: 聚合  :: 管理 ::
  55 随笔 :: 0 文章 :: 29 评论 :: 0 Trackbacks

jsp页面如下:
<html>
 <head>
  <title>jsp</title>
 <script type="text/javascript">
  var countline =1;
  /**
  *添加一行
  */
     function addRow(){
   var oldtr=tb.firstChild;
   var tr=oldtr.cloneNode(true);
   tb.appendChild(tr);
   countline++;
  }
  /**
  *删除一行
  */
   function delrow(row){
    if(countline>1){
    if(confirm("确定删除此信息?")){
     var tr=row.parentElement.parentElement;
     var index=tr.rowIndex;
     tb.deleteRow(index-1);
    }else{
     return false;
    }
    }else{
     alert("不能删除最后一行!");
    }
    countline--;
  } 
   </script>
 </head>
 <body>
  <html:form action="/trainInsertAction.do?method=add" method="post">
   <table width="80%" border="1" id="table1" cellspacing="0" cellpadding="0" align="center">
    <tr>     
     <th nowrap>站序</th>     
     <th nowrap>车站</th>          
     <th nowrap>到时</th> 
     <th nowrap>发时</th> 
     <th nowrap>里程</th>
     <th nowrap>历时</th>
     <th nowrap>硬座</th>
     <th nowrap>软座</th>
     <th nowrap>硬卧</th>
     <th nowrap>软卧</th>
     <th nowrap>操作</th>
    </tr>
    <tbody id="tb">
     <TR> 
      <TD><bean:write name="trainInsertForm" property="xh"/></TD>     
      <TD><html:text name="trainInsertForm" property="stationname" title="车站" size="12" value="" />
      </TD>
      <TD><html:text name="trainInsertForm" property="narrivetime" title="到时" size="10" value=""/></TD>
      <TD><html:text name="trainInsertForm" property="nstarttime" title="发时" size="10" value=""/></TD>     
      <TD><html:text name="trainInsertForm" property="distance" title="里程" size="10" value=""/></TD>
      <TD><html:text name="trainInsertForm" property="passtime" title="历时" size="14" value="0天0小时0分"/></TD>
      <TD><html:text name="trainInsertForm" property="hardseat" title="硬座" size="5" value=""/></TD>
      <TD><html:text name="trainInsertForm" property="softseat" title="软座" size="5" value=""/></TD>     
      <TD><html:text name="trainInsertForm" property="hardsleeper" title="硬卧" size="5" value=""/></TD>
      <TD><html:text name="trainInsertForm" property="softsleeper" title="软卧" size="5" value=""/></TD>
      <td> 
       <a href="#delete" onclick="delrow(this);return false">删除</a>  
      </td>
     </TR>
    </tbody>
    <tr>      
     <td nowrap colspan="11" align="right">
      <a href="#add" onclick="addRow();return false;">添加 </a>
     </td>      
    </tr>
    <tr>      
     <td align="center" colspan="11">
      <html:button property="button"  value=" 保存 " onclick="toCheck()"/>
     </td>      
    </tr>
   </table>
  </html:form>
 </body>
</html>

 form:
上面写的jsp实现多行提交原理,在form中把PO的所有属性定义为数组的形式,然后再写一个 方法调用PO的full constructor方法,封装成一个新的list,返回。
public class TrainInsertForm extends BaseForm {
 private static final long serialVersionUID = 1L;
 protected static TrainService trainService;
 private String[] xh;
 private Long[] nid;
 private String[] narrivetime;
...............................(其他属性和get,set方法略)
 public List<Trainnode> makeTrainNodeListAdd() {
  int length = hardseat.length;
  List<Trainnode> list = new ArrayList<Trainnode>();
    for (int i = 0; i < length; i++) {
    Trainnode trainnode = new Trainnode(new TrainnodeId(), nd
     .getNid(), nid[i], nstarttime[i], distance[i],
     hardseat[i], softseat[i], hardsleeper[i],
     softsleeper[i], passtime[i]);
   list.add(trainnode);
  }
  return list;
 }
PO类:
...............
 /** full constructor */
 public Trainnode(TrainnodeId id, Long nid, String narrivetime,
   String nstarttime, Integer distance, Float hardseat,
   Float softseat, Float hardsleeper, Float softsleeper,
   String passtime) {
.........
 }

posted on 2009-03-11 15:08 真善美 阅读(269) 评论(0)  编辑  收藏 所属分类: Java学习