springmvc如何接收前端通过表单提交的集合数据

阅读更多

1、前端代码中

在form的表单元素的name属性中,填写类似XXX[index].name的属性名,我这里采用的是thymeleaf模板框架构建的,其它前端可以做类似的修改。

 

 这样的表单,提交后,浏览器抓到的数据结构:

 

Form Data:
projectNumber: ZWI89
records[0].moneyTime: 2019-01-03
records[0].sumMoney: 239.0000
records[1].moneyTime: 2019-01-02
records[1].sumMoney: 346.0000

 2、后端的controller

 

/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	public R update(SellContractVO records) {
		System.out.println(JSON.toJSONString(records));
		// sellContractService.update(records);
		return R.ok();
	}

 controller打印出的结果

{
	"projectNumber": "ZWI89",
	"records": [{
		"moneyTime": "2019-01-03",
		"sumMoney": "239.0000"
	},
	{
		"moneyTime": "2019-01-02",
		"sumMoney": "346.0000"
	}]
}

 

 

 

3、实体类SellContractVO

 

public class SellContractVO implements Serializable {

	
	private static final long serialVersionUID = 3768978134789546577L;
	/**
	 * 项目编号
	 */
	private String projectNumber;

	/**
	 * 收付款记录
	 */
	private List records;

	public String getProjectNumber() {
		return projectNumber;
	}

	public void setProjectNumber(String projectNumber) {
		this.projectNumber = projectNumber;
	}


	public List getRecords() {
		return records;
	}

	public void setRecords(List records) {
		this.records = records;
	}

}

 

 

 

4、实体类MoneyRecord

 

package com.bootdo.contract.vo;

import java.io.Serializable;

public class MoneyRecord implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7671784309099657317L;

	/**
	 * 主键id
	 */
	private Long id;

	/**
	 * 收付款时间
	 */
	private String moneyTime;
	/**
	 * 收付款金额
	 */
	private String sumMoney;

	public MoneyRecord() {
		super();
	}

	public MoneyRecord(Long id, String moneyTime, String sumMoney) {
		super();
		this.id = id;
		this.moneyTime = moneyTime;
		this.sumMoney = sumMoney;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getMoneyTime() {
		return moneyTime;
	}

	public void setMoneyTime(String moneyTime) {
		this.moneyTime = moneyTime;
	}

	public String getSumMoney() {
		return sumMoney;
	}

	public void setSumMoney(String sumMoney) {
		this.sumMoney = sumMoney;
	}

}

 

参考链接:https://blog.csdn.net/lutinghuan/article/details/46820023

 

 

 

 

你可能感兴趣的:(springmvc,入参)