springmvc一次接受多个复杂对象

       在这之前,遇到的客户端与服务器的参数传输都是单个对象,数组、或基本数据类型,容易处理,springmvc可自动进行参数匹配。现在要一次性传多个对象,如一次性修改多个User对象。

       看了很多资料,网上上可以通过将数据转换成json方式,然后Controller直接以List接收,尝试了但是不行。

       还有一种方式就是新建一个类,将要传输的复杂对象作为该类的属性,在Controller端直接接收该对象。以下将对该方式举例:

     

      jsp页面:

 <form id="billNumberRuleUIForm"  method="post" novalidate="" role="form" class="form-horizontal"> 
  <div class="content-row"> 
  <div class="content-row">
      <div class="row"> 
        <div class="col-lg-10 col-sm-10 col-md-10">
            <button type="button" class="btn btn-primary" οnclick="saveBillNumbers();">
                <span class="glyphicon glyphicon-floppy-saved"></span>保存
            </button>
        </div> 
    </div>
    <div style="height: 150px;background: #D9EDF8;margin-top: 20px;">说明:</div>

        
        <p style="font-size: 20px;margin:30px 0px 5px 0px;">单据号设定</p>

        <div class="col-lg-12 col-sm-12 col-md-12">
        <table class="table table-bordered table-striped" style="text-align: center;">
          <thead>
            <tr>
              <th style="text-align: center;width:10%">单据号类型</th> 
              <th style="text-align: center;width:16%">单据前缀</th>
              <th style="text-align: center;width:16%">日期格式</th>
              <th style="text-align: center;width:16%">序号位数</th>
              <th style="text-align: center;width:10%">重置间隔日期</th>
              <th style="text-align: center;width:22%">单据号预览</th>
            </tr>
          </thead>
          <tbody id="tBillNumber">
          <c:forEach items="${conBillNumberRules}" var="billNumber" varStatus="status">
               <tr>
                   <input name="conBillNumberRules[${ status.index}].id"  type="hidden" value="${billNumber.id }" >
                   <td><label name="billName" >${billNumber.billName }</label></td>
                   <td>
                       <input name="conBillNumberRules[${ status.index}].prefix"  type="text"  value="${billNumber.prefix }" style="width:220px;height:32px;border-radius:5px;border:1px solid #DDDDDD;" >
                   </td>
                   <td>
                       <select  name="conBillNumberRules[${ status.index}].dateFormat" class="form-control" style="width:220px;border-radius:5px;border:1px solid #DDDDDD;">
                        <option ${billNumber.dateFormat == "" ?"selected":"" }>无</option>
                        <option ${billNumber.dateFormat == "yyyy" ?"selected":"" }>yyyy</option>
                        <option ${billNumber.dateFormat == "yyyyMM" ?"selected":"" }>yyyyMM</option>
                        <option ${billNumber.dateFormat == "yyyyMMdd" ?"selected":"" }>yyyyMMdd</option>
                    </select>
                   </td>
                   <td>
                       <select name="conBillNumberRules[${ status.index}].suffixBit" class="form-control" style="width:220px;border-radius:5px;border:1px solid #DDDDDD;">
                        <option  value="2"   ${billNumber.suffixBit == 2 ?"selected":"" }>二位</option>
                        <option  value="3"   ${billNumber.suffixBit == 3 ?"selected":"" }>三位</option>
                        <option  value="4"  ${billNumber.suffixBit == 4 ?"selected":"" }>四位</option>
                        <option  value="5"  ${billNumber.suffixBit == 5 ?"selected":"" }>五位</option>
                    </select>
                   </td>
                   <td>
                       <input name="conBillNumberRules[${ status.index}].resetIntervalDay" type="text" value="${billNumber.resetIntervalDay }" style="width:100px;height:32px;border-radius:5px;border:1px solid #DDDDDD;" >
                   </td>
                   <td data="billNumberPre" class="text-danger">
                           ${billNumber.prefix}
                   </td>
               </tr>
           </c:forEach>
          </tbody>
        </table>
    </div>
    
    </div>
    </div>
   </form>
Controller:

/**
	 * 编辑单号
	 * @return
	 */
	@RequestMapping("/edit")
	@ResponseBody
	public Map<String, Object> edit(BillNumberDTO billNumberDTO){
		Map<String, Object> map = new HashMap<>();
		boolean result = billNumberRuleService.edit(billNumberDTO);
		map.put("message", result);
		return map;
	}
新建DTO类,BillNumberDTO,用于客户端与服务器之间数据传输:

public class BillNumberDTO {
	private List<ConBillNumberRule> conBillNumberRules;//多个单号对象

	public List<ConBillNumberRule> getConBillNumberRules() {
		return conBillNumberRules;
	}

	public void setConBillNumberRules(List<ConBillNumberRule> conBillNumberRules) {
		this.conBillNumberRules = conBillNumberRules;
	}
	
}

Controller中方法中的形参即新建的数据传输类;jsp页面中要提交的表单数据,即name所对应的数据(红色标出)与Controller形参pojo对象的属性保持一致,同时对应list集合中对象的属性。
通过以上方法Controller就可以成功的接收List对象集合。


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值