2.1.springMVC JSON操作
来源:互联网 发布:我的世界java怎么安装 编辑:程序博客网 时间:2024/06/06 11:37
一、接受JSON对象字符串
在SpringMVC环境中,@RequestBody接收的是一个Json对象的字符串,而不是一个Json对象。然而在ajax请求往往传的都是Json对象,后来发现用 JSON.stringify(data)的方式就能将对象变成字符串。同时ajax请求的时候也要指定dataType: "json",contentType:"application/json" 这样就可以轻易的将一个对象或者List传到Java端,使用@RequestBody即可绑定对象或者List.
<script type="text/javascript"> $(document).ready(function(){ var saveDataAry=[]; var data1={"userName":"test","address":"gz"}; var data2={"userName":"ququ","address":"gr"}; saveDataAry.push(data1); saveDataAry.push(data2); $.ajax({ type:"POST", url:"user/saveUser", dataType:"json", contentType:"application/json", data:JSON.stringify(saveFateAry<span style="font-family: Arial, Helvetica, sans-serif;">), </span> success:function(data){ } }); }); </script>
@RequestMapping(value = "saveUser", method = {RequestMethod.POST }}) @ResponseBody public void saveUser(@RequestBody List<User> users) { userService.batchSave(users); }
二、向前台传送JSON数据
1.先配置 XXX_ servletxml
<!-- 整合jackson 返回一个json格式 --><bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"><property name="messageConverters"><list><ref bean="mappingJacksonHttpMessageConverter"/></list></property></bean><bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
2.再然后Action方法里注解就可以
<pre code_snippet_id="277836" snippet_file_name="blog_20140406_4_8928095" name="code" class="java">@RequestMapping(value="/sendShopInJSON", method = RequestMethod.POST)@ResponseBodypublic Shop sendShopInJSON(){ Shop shop = new Shop(); shop.setName(name); shop.setStaffName(new String[]{"mkyong1", "mkyong2"}); return shop; }
-----------------------
注意:JSON.stringify() : 将对象转换成json字符串。JSON.parse(): 将json字符串转换成json对象。
0 0
- 2.1.springMVC JSON操作
- jQuery操作JSON、服务端操作JSON(Servlet、SpringMVC)
- springmvc中操作json,配置FastJson
- springmvc中操作json,配置FastJson
- springmvc中操作json,配置FastJson
- SpringMVC JSON
- springmvc json
- springmvc+json
- SpringMVC Json
- SpringMVC json
- SpringMVC-JSON
- SpringMVC-json
- springmvc+json
- 随笔笔记二——SpringMVC操作json数据
- SpringMVC + Mybatis +JSON + mysql 进行批量插入的实例操作
- springMVC笔记系列(22)——Json各类操作在springMVC中的实现
- springMVC集成json
- SpringMVC返回json
- Word Ladder
- POJ 2139 Six Degrees of Cowvin Bacon(任意两点最短路,Floyd)
- 大整数操作(加、减和未完成的乘法)
- 操作系统概论总结
- iOS入门实例文章目录
- 2.1.springMVC JSON操作
- linux 修改/tmp目录的大小
- 我喜欢的文本编辑工具
- 在wxWidgets中使用ActiveX
- InvalidateRect() 函数说明
- 大学男生ATM机前忘我照镜子 银行卡被人拔走
- iOS学习之动态添加Button和监听UIAlertView按钮
- 曝福建一中学4同学谈恋爱被停课20天 校方否认
- github从0到有