ajax传递 SSM 接收多个参数包括集合对象数组等

// rows相当于java中的集合
var rows = $('#tt').datagrid('getSelections');//easyui,datagrid方法返回多行

		var testModel={};//定义一个对象
		testModel.patients=rows;//把这集合放进去
        //自定义一个对象patient也放进去
		testModel.patient={
                "name" : "xxx",
                "age": 11,
                "sex" : 1
		};


						$.ajax({
							  type: "POST",
							  url: "/patientController/test/adds",
							  data: JSON.stringify(testModel), // 这样传递到后台
							  contentType: "application/json;charset=UTF-8", // 必须
							  success: function( result ) {
								  console.log(result )
							  }
							});

// 后端java 接收
// 首先建一个与之对应的类

package com.elven.member.model;

import java.util.ArrayList;

import com.elven.member.pojo.Patient;

public class TestModel {

	private ArrayList patients;
	private Patient patient;
	public ArrayList getPatients() {
		return patients;
	}
	public void setPatients(ArrayList patients) {
		this.patients = patients;
	}
	public Patient getPatient() {
		return patient;
	}
	public void setPatient(Patient patient) {
		this.patient = patient;
	}

	
}

//controller

	/**
	 * 测试接收数组
	 * 对象
	 * */
	@RequestMapping("/test/adds")
	@ResponseBody
	public String testAdds(@RequestBody TestModel testModel) {
		ArrayList patients = testModel.getPatients();
		System.out.println("patient---------"+testModel.getPatient());
		for (Patient patient : patients) {
			System.out.println(patient);
		}
		String str = "成功!";
		return str;
	}

参考博客,论坛

https://bbs.csdn.net/topics/392190087?page=1

你可能感兴趣的:(Java)