做web开发的一直遇到一个问题怎么发请求带各种类型的参数到Controller层,每次都要测试好久才能请求成功。今天趁有空查资料总结巩固一下。
一 .@RequestBody和@RequestParam区别
1.@RequestParam
用来处理Content-Type: 为 application/x-www-form-urlencoded编码的内容。(Http协议中,如果不指定Content-Type,则默认传递的参数就是application/x-www-form-urlencoded类型)
RequestParam可以接受简单类型的属性,也可以接受对象类型。
实质是将Request.getParameter() 中的Key-Value参数Map利用Spring的转化机制ConversionService配置,转化成参数接收对象或字段。
2.@RequestBody
处理HttpEntity传递过来的数据,一般用来处理非Content-Type: application/x-www-form-urlencoded编码格式的数据。
- GET请求中,因为没有HttpEntity,所以@RequestBody并不适用。
- POST请求中,通过HttpEntity传递的参数,必须要在请求头中声明数据的类型Content-Type,SpringMVC通过使用HandlerAdapter 配置的HttpMessageConverters来解析HttpEntity中的数据,然后绑定到相应的bean上。
3.@ModelAttribute
该注解有两个用法,一个是用于方法上,一个是用于参数上;
用于方法上时: 通常用来在处理@RequestMapping之前,为请求绑定需要从后台查询的model;
用于参数上时:用来通过名称对应,把相应名称的值绑定到注解的参数bean上;要绑定的值来源于:
- A) @SessionAttributes 启用的attribute 对象上;
- B) @ModelAttribute 用于方法上时指定的model对象;
- C) 上述两种情况都没有时,new一个需要绑定的bean对象,然后把request中按名称对应的方式把值绑定到bean中。
二.一些常用的传参类型
1.字符串类型参数
getStringParam:function(){
var param={"stringParams":hidden_input_value};
//var param={};两种方式都行
// param.stringParams=hidden_input_value;
$.ajax({
url:basePath+"/ParamFormatController/getStringParam.shtml",
data:param,
type : 'POST',
dataType : 'json',
success:function(data){
},
})
},
Controller层
@RequestMapping("/getStringParam")
@ResponseBody
public String getStringParam(String userName) {
String password = userService.getUserpassword(userName);
return password;
}
2.对象类型
setObjectParam:function(){
var objectParams={};
objectParams.name="张三";
objectParams.age="19";
objectParams.sex="男";
$.ajax({
url:basePath+"/ParamFormatController/getObjectParam.shtml",
data:objectParams,
type : 'POST',
dataType : 'json',
success:function(data){
},
})
},
@RequestMapping("/getObjectParam")
@ResponseBody
public void getObjectParam(Student objectParams) {
System.out.println("姓名:" + objectParams.getName());
System.out.println("性别:" + objectParams.getSex());
System.out.println("年龄:" + objectParams.getAge());
}
3.复杂对象类型
setHardObjectParam:function(){
var objectParams = {
schoolName:"鹏峰中学",
data:new Date(),
teacherNames:["张老师","李老师","王老师"],
students:[
{name:"小明",sex:"男",age:"19"},
{name:"小红",sex:"男",age:"19"},
]
};
$.ajax({
type: "POST",
url:basePath+"/ParamFormatController/getHardObjectParam.shtml",
data: JSON.stringify(objectParams),//重点
contentType:"application/json" //指定类型
})
},
School类
public class School {
private String[] teacherNames;
private String schoolName;
private Date data;
private List<Student> students;
public String[] getTeacherNames() {
return teacherNames;
}
public void setTeacherNames(String[] teacherNames) {
this.teacherNames = teacherNames;
}
public String getSchoolName() {
return schoolName;
}
public void setSchoolName(String schoolName) {
this.schoolName = schoolName;
}
public Date getData() {
return data;
}
public void setData(Date data) {
this.data = data;
}
public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
}
Controoler
@RequestMapping("/getHardObjectParam")
@ResponseBody
public void getHardObjectParam(@RequestBody School objectParams) {
System.out.println("学校名:" + objectParams.getSchoolName());
}
4.数组与字符串
getArrayAndStringParam:function(){
var pointCodes= new Array(); //定义一数组
pointCodes.push("1");
pointCodes.push("2");
pointCodes.push("3");
pointCodes.push("4");
$.ajax({
url:basePath+"/ParamFormatController/getArrayAndStringParam.shtml",
type:"POST",
data:{"names":pointCodes,"id":'6'},
dataType:"json",
success:function(res){
}
});
},
@RequestMapping("/getArrayAndStringParam")
@ResponseBody
public void getArrayAndStringParam(@RequestParam(value = "names[]") String[] names, String id) {
for (int i = 0; i < names.length; i++) {
System.out.println(names[i]);
}
}
5.List字符串集合
getListByStringParam:function(){
var orderNosList = new Array();
orderNosList.push("List1");
orderNosList.push("List2");
orderNosList.push("List3");
orderNosList.push("List4");
$.ajax({
url:basePath+"/ParamFormatController/getListByStringParam.shtml",
data:{"StringList":orderNosList},
type : 'POST',
dataType : 'json',
success:function(data){
},
})
},
@RequestMapping("/getListByStringParam")
@ResponseBody
public void getListByStringParam(@RequestParam("StringList[]") List<String> list) {
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
6.List对象集合
@RequestMapping(value = "/getListByObjectParam")
@ResponseBody
public void getListByObjectParam(@RequestBody List<Student> students) {
for (int i = 0; i < students.size(); i++) {
Student s = students.get(i);
System.out.println(s.getName());
}
}
getListByObjectParam:function(){
var param=[];
var data1={"name":"张三","age":"21","sex":"2"};
var data2={"name":"李四","age":"24","sex":"2"};
param.push(data1);
param.push(data2);
$.ajax({
url:basePath+"/ParamFormatController/getListByObjectParam.shtml",
data:JSON.stringify(param),
type : 'POST',
contentType: "application/json",
success:function(data){
},
})
},
后续类型在补充。
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。