史上最全的springmvc入参传递总结
一、springmvc的优势
1、springmvc能够将URL从http的世界中映射到JAVA世界中,这是框架的核心功能,不得不说确实很强大,但非常的容易理解。
2、springmvc对annotation的完没支持,去掉struts2及springbean繁琐的配置文件,提高开发效率。
3、springmvc结合jackson-core,action层只用关心业务对象的编写,不用关心入参的转换(json->对象)及返回值的转换(对象->json)。
二、Action方法入参的接收
1、入参为简单对象,java.lang.String、java.lang.Integer
Ajax请求数据
function simpleObject() { /*必须与后端参数名保持一致*/ var data = { "name": "张三", "age": 12 }; var url = "<%=request.getContextPath()%>" + "/v1/simpleObject.action"; $.ajax({ url: url, type: "POST", data: data, dataType: "text", success: function (result) { alert(result); } }); }
Action接收
@RequestMapping(value = "/simpleObject", method = RequestMethod.POST) @ResponseBody public String simpleObject(String name, Integer age) { System.out.println("学生的姓名:" + name + ";年龄:" + age); return name + ":" + age; }
2、入参为自定义对象,Student
Ajax请求
function customObject() { /*必须与自定义对象的属性名保持一致*/ var data = { "name": "张三", "age": 12 }; var url = "<%=request.getContextPath()%>" + "/v1/customObject.action"; $.ajax({ url: url, type: "POST", data: data, dataType: "text", success: function (result) { alert(result); } }); }
Action接收
@RequestMapping(value = "/customObject", method = RequestMethod.POST) @ResponseBody public String customObject(Student student) { String name = student.getName(); Integer age = student.getAge(); System.out.println("学生的姓名:" + name + ";年龄:" + age); return name + ":" + age; }
3、入参为简单对象(String, Integer)和自定义对象Student混合
Ajax请求
function mixedObject() { /*name、age必须和Student的属性一致,className、classNum必须和后台参数名一致*/ var data = { "name": "张三", "age": 12, "className" : "二年级", "classNum" : 3 }; var url = "<%=request.getContextPath()%>" + "/v1/mixedObject.action"; $.ajax({ url: url, type: "POST", data: data, dataType: "text", success: function (result) { alert(result); } }); }
Action接收
@RequestMapping(value = "/mixedObject", method = RequestMethod.POST) @ResponseBody public String mixedObject(Student student, String className, Integer classNum) { String stuName = student.getName(); Integer age = student.getAge(); System.out.println("学生的姓名:" + stuName + ";年龄:" + age); System.out.println("年级:" + className + ";班级数:" + classNum); return stuName + ":" + age + ":" + className + ":" + classNum; }
4、入参为简单对象(String, Integer)列表参数传递
Ajax请求
function simpleObjectList() { /*必须与自定义对象的属性名保持一致*/ var data = { "ids" : [1, 2, 3, 4] }; var url = "<%=request.getContextPath()%>" + "/v1/simpleObjectList.action"; $.ajax({ url: url, type: "POST", data: data, dataType: "text", success: function (result) { alert(result); } }); }
Action接收
@RequestMapping(value = "/simpleObjectList", method = RequestMethod.POST) @ResponseBody public String simpleObjectList(@RequestParam(value = "ids[]", required = false) List<Integer> ids) { //java8新特性,通过流循环集合 ids.stream().forEach(id -> System.out.print(id) ); return "success"; }
tips:@RequestParam标签的value值需要和前端传递的参数名保持一致,如前端参数名为ids,标签的value的值应为“ids[]”,建议加上required=false属性,因为当前端无ids参数或参数值为空时,不配置required=false属性,springmvc会抛出一个异常。
5、入参为简单对象(String, Integer)、自定义对象Student和简单对象(String, Integer)列表参数传递
Ajax请求
function mixedSimpleObjectList() { /*必须与自定义对象的属性名保持一致*/ var data = { "ids" : [1, 2, 3, 4], "name": "张三", "age": 12, "className" : "二年级", "classNum" : 3 }; var url = "<%=request.getContextPath()%>" + "/v1/mixedSimpleObjectList.action"; $.ajax({ url: url, type: "POST", data: data, dataType: "text", success: function (result) { alert(result); } }); }
Action接收
@RequestMapping(value = "/mixedSimpleObjectList", method = RequestMethod.POST) @ResponseBody public String mixedSimpleObjectList(@RequestParam(value = "ids[]", required = false) List<String> ids, Student student, String className, Integer classNum) { ids.stream().forEach(id -> System.out.print(id) ); String stuName = student.getName(); Integer age = student.getAge(); System.out.println("学生的姓名:" + stuName + ";年龄:" + age); System.out.println("年级:" + className + ";班级数:" + classNum); return "success"; }
6、入参为单个自定义对象(Student)列表参数传递,通过数组方式
Ajax请求
function customObjectList() { var studentArray = []; var student = new Object(); student.name = "张三"; student.age = 12; studentArray.push(student); var student1 = new Object(); student1.name = "李四"; student1.age = 13; studentArray.push(student1); var url = "<%=request.getContextPath()%>" + "/v1/customObjectList.action"; $.ajax({ url: url, type: "POST", data: JSON.stringify(studentArray), //将数组转化为json字符串 dataType: "text", contentType: "application/json; charset=utf-8", success: function (result) { alert(result); } }); }
Action接收
@RequestMapping(value = "/customObjectList", method = RequestMethod.POST) @ResponseBody public String customObjectList(@RequestBody List<Student> list) { try { list.stream().forEach(student -> System.out.println("name:" + student.getName() + " ;age:" + student.getAge()) ); } catch (Exception e) { e.printStackTrace(); } return "success"; }
tips:springmvc支持自定义对象列表的参数传递是使用json串
前端开发人员需要了解两点:
1、HttpServletRequest对象的Content-Type属性,一般情况下,Content-Type的值有以下两种:
Content-Type | 含义 |
application/x-www-form-urlencoded;charser=utf-8 | 请求参数以form表单的形式提交 |
application/json;charset=utf-8 | 请求参数以json传的形式提交 |
Content-TYpe为application/x-www-form-urlencoded;charser=utf-8是jquery ajax、post等方法的默认行为,大多数的业务,采用这种方式就可以满足我们的需求。
本次请求传递的是json传,所以
2、jquery的JSON.stringify函数的含义:它会将前端参数转变为json传
后端开发人员需要了解:contentType: "application/json; charset=utf-8"
@RequestBody的含义:将json串转变为对象。
7、入参为单个自定义对象列表参数传递,通过DTO的方式
Ajax请求
function customObjectListByDto() { /!*必须与自定义对象的属性名保持一致*!/ var data = { "studentList" : [ {"name" : "张三", "age" : 12}, {"name" : "李四", "age" : 13} ] }; var url = "<%=request.getContextPath()%>" + "/v1/customObjectListByDto.action"; $.ajax({ url: url, type: "POST", data: JSON.stringify(data), //将数组转化为json字符串 dataType: "text", contentType: "application/json; charset=utf-8", success: function (result) { alert(result); } }); }
Action接收
@RequestMapping(value = "/customObjectListByDto", method = RequestMethod.POST) @ResponseBody public String customObjectListByDto(@RequestBody StudentDto studentDto) { try { studentDto.getStudentList().stream().forEach(student -> System.out.println("name:" + student.getName() + " ;age:" + student.getAge()) ); } catch (Exception e) { e.printStackTrace(); } return "success"; }
tips:自定义对象列表的参数传递,springmvc目前只能支持前端传递json串,后台使用DTO这种方式,对于这种传值方式,后端@RequestBody标签是必不可少的,它会告诉springmvc,需要将前端提交的数据映射到DTO的对象属性上。
8、入参为多个自定义对象列表和简单对象参数传递,通过DTO的方式
Ajax请求
function mixedCustomObjectListByDto() { /!*必须与自定义对象的属性名保持一致*!/ var data = { "schoolName" : "东方完小", "year" : 2018, "studentList" : [ {"name" : "张三", "age" : 12}, {"name" : "李四", "age" : 13} ], "gradeList" : [ {"name" : "二年级", "classNum" : 2}, {"name" : "三年级", "classNum" : 3} ] }; var url = "<%=request.getContextPath()%>" + "/v1/mixedCustomObjectListByDto.action"; $.ajax({ url: url, type: "POST", data: JSON.stringify(data), //将数组转化为json字符串 dataType: "text", contentType: "application/json; charset=utf-8", success: function (result) { alert(result); } }); }
Action接收
@RequestMapping(value = "/mixedCustomObjectListByDto", method = RequestMethod.POST) @ResponseBody public String mixedCustomObjectListByDto(@RequestBody StudentDto studentDto) { studentDto.getStudentList().stream().forEach(student -> System.out.println("name:" + student.getName() + " ;age:" + student.getAge()) ); studentDto.getGradeList().stream().forEach(grade -> System.out.println("className:" + grade.getName() + " ;classNum:" + grade.getClassNum()) ); return "success"; }
tips:springmvc不支持json到多个DTO对象的mapping映射。
9、StudentDto.java、Student.java、Grade.java
public class StudentDto { private String schoolName; private Integer year; private List<Student> studentList; private List<Grade> gradeList; public String getSchoolName() { return schoolName; } public void setSchoolName(String schoolName) { this.schoolName = schoolName; } public Integer getYear() { return year; } public void setYear(Integer year) { this.year = year; } public List<Student> getStudentList() { return studentList; } public void setStudentList(List<Student> studentList) { this.studentList = studentList; } public List<Grade> getGradeList() { return gradeList; } public void setGradeList(List<Grade> gradeList) { this.gradeList = gradeList; } } public class Student { private Integer id; private String name; private Integer age; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } } public class Grade { private Integer id; private String name; private Integer classNum; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getClassNum() { return classNum; } public void setClassNum(Integer classNum) { this.classNum = classNum; } }
代码已上传github:https://github.com/architectboy/ssm-web/releases/tag/ssm-web-1.0
总结不易,转发请表明出处:https://www.cnblogs.com/19940330a/p/9873123.html