Spring MVC表单提交多个对象

这篇博客讲述了在使用Spring MVC处理动态添加行的表单提交时,如何避免因为空值导致的问题。文章通过`DeliverApplyDetailListForm`类展示了如何定义包含多个`DeliverApplyDetail`对象的列表,并在Controller中遍历并检查每个对象的字段,只插入非空的对象到数据库。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

动态添加行时有可能提交的时候出现空值,到时候只要判断其中的字段是否为空,然后new一个新集合,把不为空的对象放进去就可以了。


1、

public class DeliverApplyDetailListForm {
    private List<DeliverApplyDetail> deliverApplyDetail;

    public List<DeliverApplyDetail> getDeliverApplyDetail() {
        return deliverApplyDetail;
    }

    public void setDeliverApplyDetail(List<DeliverApplyDetail> deliverApplyDetail) {
        this.deliverApplyDetail = deliverApplyDetail;
    }
    
}

2、

package cn.fenghuait.sale.model;

import java.math.BigDecimal;

public class DeliverApplyDetail {
    private String detailNo;

    private String productId;

    private Long productNeedNum;

    private BigDecimal realPrice;

    private BigDecimal ticketPrice;

    private BigDecimal totalRealPrice;

    private BigDecimal totalTicketPrice;

    private String applyNo;

    private String typeName;

    public String getDetailNo() {
        return detailNo;
    }

    public void setDetailNo(String detailNo) {
        this.detailNo = detailNo;
    }

    public String getProductId() {
        return productId;
    }

    public void setProductId(String productId) {
        this.productId = productId;
    }

    public Long getProductNeedNum() {
        return productNeedNum;
    }

    public void setProductNeedNum(Long productNeedNum) {
        this.productNeedNum = productNeedNum;
    }

    public BigDecimal getRealPrice() {
        return realPrice;
    }

    public void setRealPrice(BigDecimal realPrice) {
        this.realPrice = realPrice;
    }

    public BigDecimal getTicketPrice() {
        return ticketPrice;
    }

    public void setTicketPrice(BigDecimal ticketPrice) {
        this.ticketPrice = ticketPrice;
    }

    public BigDecimal getTotalRealPrice() {
        return totalRealPrice;
    }

    public void setTotalRealPrice(BigDecimal totalRealPrice) {
        this.totalRealPrice = totalRealPrice;
    }

    public BigDecimal getTotalTicketPrice() {
        return totalTicketPrice;
    }

    public void setTotalTicketPrice(BigDecimal totalTicketPrice) {
        this.totalTicketPrice = totalTicketPrice;
    }

    public String getApplyNo() {
        return applyNo;
    }

    public void setApplyNo(String applyNo) {
        this.applyNo = applyNo;
    }

    public String getTypeName() {
        return typeName;
    }

    @Override
    public String toString() {
        return "DeliverApplyDetail [detailNo=" + detailNo + ", productId="
                + productId + ", productNeedNum=" + productNeedNum
                + ", realPrice=" + realPrice + ", ticketPrice=" + ticketPrice
                + ", totalRealPrice=" + totalRealPrice + ", totalTicketPrice="
                + totalTicketPrice + ", applyNo=" + applyNo + ", typeName="
                + typeName + "]";
    }

    public void setTypeName(String typeName) {
        this.typeName = typeName;
    }
    
}

3、Controller

 @RequestMapping(value="/deliverSaleApply")
      @ResponseBody
      public Map<String, Object> deliverSaleApply(HttpServletRequest request,DeliverApplyDetailListForm deliverApplyDetails,DeliverApply deliverApply){

for(DeliverApplyDetail deliverApplyDeatail :deliverApplyDetails.getDeliverApplyDetail()){
                  deliverApplyDeatailService.insert(deliverApplyDeatail);
              }

}


4、jsp页面


<table width="752" border="1px;" align="center" style="border-collapse:collapse;border:1px solid black;margin:auto;" id="tb1">
  <tr>
    <td width="54" rowspan="80">申请发货明细</td>
    <td width="82" rowspan="2" height="40px;">产品名称</td>
    <td width="65" rowspan="2">规格</td>
    <td colspan="2">数量</td>
    <td colspan="2">单价</td>
    <td colspan="2">结算贷款</td>
  </tr>
  <tr>
    <td width="54">盒/瓶</td>
    <td width="53">件</td>
    <td width="56">实价(元)</td>
    <td width="63">开票价(元)</td>
    <td width="72" >实价合计(元)</td>
    <td width="85" onclick="deleteRow()">开票价合计(元)</td>
  </tr>
  <tr id="a1">
    <td height="20px;">
       <select id="productId" name="deliverApplyDetail[0].productId" style="width: 140px;height: 22px; text-align: center;">
       <c:forEach items="${productIdNames}" var="p">
      <option value="${p.productId }">${p.commName }</option>
      </c:forEach>
    </select>
    </td>
    <td>
    <select id="typeName" name="deliverApplyDetail[0].typeName" style="width: 140px;height: 22px; text-align: center;">
    <c:forEach items="${typeInfos }" var="ti">
      <option value="${ti.typeName }"> ${ti.typeName }</option>
      </c:forEach>
    </select>
    </td>
    <td>
    <select id="" name="" style="width: 70px;height: 22px; text-align: center;">
      <option value="0"> 盒</option>
      <option value="1"> 瓶</option>
    </select>
    </td>
    <td><input  name="deliverApplyDetail[0].productNeedNum" id="productNeedNum0" type="text"  style=" width:89%;height: 18px;" value="0" /></td>
    <td><input  name="deliverApplyDetail[0].realPrice" id="realPrice0" type="text"  style=" width:89%;height: 18px;" value="0" /></td>
    <td><input  name="deliverApplyDetail[0].ticketPrice" id="ticketPrice0" type="text"  style=" width:91%;height: 18px;"  value="0" /></td>
    <td><input  name="deliverApplyDetail[0].totalRealPrice" id="totalRealPrice0" type="text"  style=" width:92%;height: 18px;" value="0" /></td>
    <td id="td1"><input  name="deliverApplyDetail[0].totalTicketPrice" id="totalTicketPrice0" type="text"  style=" width:92.5%;height: 18px;" value="0"/></td>
  </tr>
  <tr>
    <td >合计</td>
    <td height="18px;">&nbsp;</td>
    <td>&nbsp;</td>
    <td><input id="NumTotal" name="NumTotal" type="text" value="0" style="width: 89%;"/></td>
    <td><input id="sumRealPrice" name="sumRealPrice" type="text" value="0" style="width: 89%;"/></td>
    <td><input id="sumTicketPrice" name="sumTicketPrice" type="text" value="0" style="width: 90%;"/></td>
    <td><input id="sumTotalRealPrice" name="sumTotalRealPrice" type="text" value="0" style="width: 92%;"/></td>
    <td><input id="sumTotalTicketPrice" name="sumTotalTicketPrice" type="text" value="0" style="width: 93%;"/></td>
  </tr>
</table>


--------------------------------------下面为动态添加行----------------------------------------------------------

$(document).ready(function(){
    $("#td1").live("click",function(){
        i++;
      var x=document.getElementById('tb1').insertRow(3);
      var a=x.insertCell(0)
      var b=x.insertCell(1)
       var c=x.insertCell(2)
       var d=x.insertCell(3)
       var e=x.insertCell(4)
       var f=x.insertCell(5)
       var g=x.insertCell(6)
       var h=x.insertCell(7)
       var a1 = "<select id='productId' name='deliverApplyDetail["+i+"].productId' style='width: 140px;height: 22px; text-align: center;'><c:forEach items='${productIdNames}' var='p'><option value='${p.productId }'>${p.commName }</option></c:forEach></select>";
       var b1="<select id='typeName' name='deliverApplyDetail["+i+"].typeName' style='width: 140px;height: 22px; text-align: center;'><c:forEach items='${typeInfos }' var='ti'><option value='${ti.typeName }'> ${ti.typeName }</option></c:forEach> </select>";
       var c1 = "<select id='' name='' style='width: 70px;height: 22px; text-align: center;'><option value='0'> 盒</option><option value='1'> 瓶</option></select>";
       var d1="<input  name='deliverApplyDetail["+i+"].productNeedNum' id='productNeedNum"+i+"' type='text'  style='width:89%;height: 18px' value='0' />";
       var e1='<input  name="deliverApplyDetail['+i+'].realPrice" id="realPrice'+i+'" type="text"  style=" width:89%;height: 18px;" value="0" />';
      var f1='<input   name="deliverApplyDetail['+i+'].ticketPrice" id="ticketPrice'+i+'" type="text"  style=" width:91%;height: 18px;"  value="0" />';
      var g1='<input  name="deliverApplyDetail['+i+'].totalRealPrice" id="totalRealPrice'+i+'" type="text"  style=" width:92%;height: 18px;" value="0"  />';
      var h1="<input   name='deliverApplyDetail["+i+"].totalTicketPrice' id='totalTicketPrice"+i+"' type='text'  style=' width:92.5%;height: 18px;' value='0'/>";
      a.innerHTML=a1
      b.innerHTML=b1
      c.innerHTML=c1
      d.innerHTML=d1
      e.innerHTML=e1
      f.innerHTML=f1
      g.innerHTML=g1
      h.innerHTML=h1
     });
});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值