ajax向springmvc传送对象参数
我们在使用前端的ajax技术过程中,有的时候简简单单的向后台的springmvn传递参数,直接使用如下代码即可:(jquery的ajax代码)
var options = { url: 'helloworld', method: 'get', dataType: 'json', data: { teamId: 123 }, success: function (rs) { // code here }, error: function (rs) { // code here } }; $.ajax(options);
后台springmvc代码:
@RequestMapping(value = { "/helloworld" }, method = { RequestMethod.GET }) @ResponseBody public Boolean helloworld(@RequestParam("teamId") Long teamId) { LOGGER.debug("start to helloworld. teamId:{}", teamId); Boolean rs = null; // coder here LOGGER.debug("finish helloworld. teamId:{}", teamId); return rs; }
上面也是常用的情况。
但是有的时候我们需要用ajax向后台传递对象,该如何做呢?前端传递的是JavaScript的对象,而后台接收的是java的对象。springmvn为我们做好了由JavaScript对象到Java对象的json转换。如下:
var paramsArr = []; for(var i = 0; i < 10; i++) { var obj = { id: i, name: 'name-' + i }; paramsArr.push(obj); } var options = { url: 'helloworld', method: 'post', // 注意这里,传递对象给后台,这里必须是 POST 否则无法将对象封装到POST的body中流 dataType: 'json', contentType: 'application/json', // 注意这里,传递对象给后台,这里必须是 application/json data: JSON.stringify(paramsArr), // 注意这里,传递对象给后台,这里必须将对象进行序列化 success: function (rs) { // code here }, error: function (rs) { // code here } }; $.ajax(options);
class Person { private Long id; private String name; // setter / getter here } /** * * 注意参数中的 @RequestBody。 它将会读取POST请求中的body中的数据流,然后JSON反序列化为java的对象。 */ @RequestMapping(value = { "/helloworld" }, method = { RequestMethod.POST }) @ResponseBody public Boolean helloworld(@RequestBody List<Person> persons) { LOGGER.debug("start to helloworld. persons:{}", persons); Boolean rs = null; // coder here LOGGER.debug("finish helloworld. persons:{}", persons); return rs; }
这样对象参数就能传递给后台了。