Currently, I am utilizing spring MVC and have the requirement to transmit data to my controller on the backend server.
@RequestMapping(value="/project/update")
public @ResponseBody projectWebForm update(HttpServletRequest request,
HttpServletResponse response,
@RequestBody projectWebForm input){
..............
}
To accomplish this task, I am making use of the @RequestBody annotation to bind my fields to a class named projectWebForm
Inside the projectWebForm class, there are 3 fields
private String name;
private String age;
private List<List<Data>> list;
In addition, I have defined a Data class as follows
public class Data{
private String data1;
private String data2;
getters setters.....
}
Now, in a jsp file, I am implementing ajax functionality to send data to the controller
function update(name,age){
var option={
headers:{'Content-type':'application/json;charset=utf-8'},
contentType:'application/json;charset=utf-8',
dataType:'json',
async:true,
type:'post',
data:JSON.stringify({
name:name,
age:age,
list:____________ <---- ????????
}),
url:'<%=request.getContextPath()%>/project/update.do',
success:function(response){
......
}
},
};
$.ajax(option);
};
I have successfully passed the name and age to the controller since they are both Strings. However, now comes the challenge of transmitting a list of lists. How can I structure my list so that it can be mapped correctly to the RequestBody? What should the JSON format be for sending this information to the RequestBody? Can you provide an example?