目录一 .@RequestBody和@RequestParam区别1.@RequestParam2.@RequestBody3.@ModelAttribute二.一些常用的传参类型&
做web开发的一直遇到一个问题怎么发请求带各种类型的参数到Controller层,每次都要测试好久才能请求成功。今天趁有空查资料总结巩固一下。
用来处理Content-Type: 为 application/x-www-fORM-urlencoded编码的内容。(Http协议中,如果不指定Content-Type,则默认传递的参数就是application/x-www-form-urlencoded类型)
RequestParam可以接受简单类型的属性,也可以接受对象类型。
实质是将Request.getParameter() 中的Key-Value参数Map利用spring的转化机制ConversionService配置,转化成参数接收对象或字段。
处理HttpEntity传递过来的数据,一般用来处理非Content-Type: application/x-www-form-urlencoded编码格式的数据。
该注解有两个用法,一个是用于方法上,一个是用于参数上;
用于方法上时: 通常用来在处理@RequestMapping之前,为请求绑定需要从后台查询的model;
用于参数上时:用来通过名称对应,把相应名称的值绑定到注解的参数bean上;要绑定的值来源于:
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;
}
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());
}
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());
}
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]);
}
}
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));
}
}
@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){
},
})
},
后续类型在补充。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。
--结束END--
本文标题: js传各种类型参数到Controller层的整理方式
本文链接: https://lsjlt.com/news/197632.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-01-12
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0