SpringMVC 前后台传参(对象集合,Map参数)

来源:互联网 发布:淘宝助理连打设置 编辑:程序博客网 时间:2024/06/03 15:36

1.传递对象集合:

  • form表单name命名语法规则: 请求对象属性名[n].字段名字 = value; ,如
< input type="text" name="conditionList[0].tableId">

请求报文的结构为:conditionList[0].tableId=table1&conditionList[1].tableId=table2

这里写图片描述

<div>   <div>   <div>     <div>         <span class="input-group-addon">表1</span>         <input type="text" name="conditionList[0].tableId" >     </div>     <div>       <span class="input-group-addon">表2</span>       <input type="text" name="conditionList[1].tableId" >     </div>   </div>   <div>     <div>       <button type="button" id="query1" name="query1" onclick="queryTest()"> </button>     </div>   </div> </div>
  • 后台JavaBean:
/***请求对象**/public class MyTestRequest{    private List<ReportQueryCondition> conditionList;    public List<ReportQueryCondition> getConditionList() {        return conditionList;    }    public void setConditionList(List<ReportQueryCondition> conditionList) {        this.conditionList = conditionList;    }}/***映射对象**/public class ReportQueryCondition{/**     * 表名字     */    private String tableId;    public String getTableId() {        return tableId;    }    public void setTableId(String tableId) {        this.tableId = tableId;    }}

2.单个Map对象

  • form表单name命名语法规则: 请求对象属性名[key] = value;
    < input type="text" name="sort[table]" >
    多个输入框name相同,会自动把值拼接成以逗号隔开的字符串
    参数报文结构:sort[table]=table1+&sort[field]=field1&sort[type]=desc

这里写图片描述

  • form表单代码:
<div>       <div>            <span class="input-group-addon">排序表</span>              <input type="text" id="id" name="sort[table]"   >       </div>        <div>              <span class="input-group-addon">排序字段</span>              <input type="text" id="id" name="sort[field]" >        </div>         <div >                <span class="input-group-addon">排序方式</span>                <input type="text" id="name" name="sort[type]" >        </div> </div><div class="form-group">        <div class="col-sm-offset-2 col-sm-10">       <button type="button" id="query1" name="query1" onclick="queryTest()">                                    查询      </button></div>
  • 后台Java代码:
/***请求对象**/public class MyTestRequest{   private Map<String,Object> sort;  //排序    public Map<String, Object> getSort() {        return sort;    }    public void setSort(Map<String, Object> sort) {        this.sort = sort;    }}

这里写图片描述

2.多个Map对象

  • form表单name命名语法规则: 请求对象属性名[n][key] = value;
    < input type="text" id="id" name="sortList[0][table]" >
    报文结构:
    sortList[0][table]=table1&sortList[0][field]=field1&sortList[1][table]=table2&sortList[1][field]=field2

这里写图片描述

  • form 表单代码:
<div >                            <div class="col-xs-3 input-group">                                <span class="input-group-addon">排序表1</span>                                <input type="text"  name="sortList[0][table]" >                            </div>                            <div class="col-xs-3 input-group">                                <span class="input-group-addon">排序字段1</span>                                <input type="text"  name="sortList[0][field]" >                            </div>                            <div class="col-xs-3 input-group">                                <span class="input-group-addon">排序表2</span>                                <input type="text"  name="sortList[1][table]"  >                            </div>                            <div class="col-xs-3 input-group">                                <span class="input-group-addon">排序字段2</span>                                <input type="text"  name="sortList[1][field]"  >                            </div>                        </div>          <div class="form-group">              <div class="col-sm-offset-2 col-sm-10">                  <button type="button" id="query1" name="query1" onclick="queryTest()">                      查询                  </button>          </div>
  • Java代码:
/***请求对象**/public class MyTestRequest{  private List<Map<String,Object>> sortList; //排序集合    public List<Map<String, Object>> getSortList() {        return sortList;    }    public void setSortList(List<Map<String, Object>> sortList) {        this.sortList = sortList;    }}

这里写图片描述

1 0
原创粉丝点击