SpringMVC传递json格式数据
2015-05-26 09:50
337 查看
SpringMVC的@ResponseBody可以用来返回类似json格式的数据,此时,需要定义一个额外的dto,其实也就是一个类似于model层的封装,它内部维护的是所有关于json要返回的数据的属性,其他无用的就不用写。(1)web层,@ResponseBody AjaxObj返回的是dto相对应的json数据@RequestMapping("/channels/updateSort")public @ResponseBody AjaxObj updateSort(@Param Integer[] ids) {try {channelService.updateSort(ids);} catch (Exception e) {return new AjaxObj(0,e.getMessage());}return new AjaxObj(1);}(2)dto层/*** 专门用来返回Ajax处理之后结果json数据的对象* @author Administrator**/public class AjaxObj {/*** 0表示失败* 1表示成功*/private int result;/*** 提示信息*/private String msg;/*** 附加对象,用来存储一些特定的返回信息*/private Object obj;public int getResult() {return result;}public void setResult(int result) {this.result = result;}public String getMsg() {return msg;}public void setMsg(String msg) {this.msg = msg;}public Object getObj() {return obj;}public void setObj(Object obj) {this.obj = obj;}public AjaxObj() {}public AjaxObj(int result, String msg, Object obj) {super();this.result = result;this.msg = msg;this.obj = obj;}public AjaxObj(int result, String msg) {super();this.result = result;this.msg = msg;}public AjaxObj(int result) {super();this.result = result;}}(3)前端
(function($){ <span style="white-space:pre"> </span>$.ajaxCheck = function(data) { <span style="white-space:pre"> </span>if(data.result) return true; <span style="white-space:pre"> </span>else { <span style="white-space:pre"> </span>alert(data.msg); <span style="white-space:pre"> </span>return false; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>}
<pre name="code" class="html">$.fn.mysorttable = function(opts) {
<span> </span>function saveOrders() { if(_isSort) { var idArg = sortEle.sortable("serialize",{key:"ids"}); $.post("updateSort?"+idArg,function(data){ if($.ajaxCheck(data)) { $(_that).find("tr").each(function(){ $(this).children().last().remove(); }); sortEle.sortable("disable"); _isSort = false; } /* if(data.result) { $(_that).find("tr").each(function(){ $(this).children().last().remove(); }); sortEle.sortable("disable"); _isSort = false; } else { alert(data.msg); }*/ }); } else { alert("还不是排序状态"); } }
}})(jQuery)
相关文章推荐
- SpringMVC框架下JQuery传递并解析Json格式的数据是如何实现的
- SpringMVC框架下JQuery传递并解析Json格式的数据
- springMVC传递JSON格式数据
- SpringMVC框架下JQuery传递并解析Json格式的数据是如何实现的
- springMVC中使用json格式传递数据
- springMVC 前台传递json格式数据到后台
- SpringMVC处理Ajax请求(请求数据为 json 格式字符串)
- 调用WCF传递JSON格式数据
- 客户端传递json格式数据,spring mvc服务端接受并进行参数校验
- SpringMVC使用@RequestBody与@ResponseBody注解与前台进行json格式的数据
- SpringMVC环境下实现的Ajax异步请求(JSON格式数据) 推荐
- SpringMvc+ajax 实现json格式数据传递
- Spring3+Hibernate4+SpringMVC整合Ext:JSON数据格式传输
- springMVC框架下JQuery传递并解析Json数据
- Angularjs cors 用get、post方法与springmvc后传递json数据的配置
- 160504、springmvc返回json数据编码格式设定
- asp.net中Ajax技术使用Json格式传递数据
- 通过gradle生成SpringMVC实现RESTful返回JSON格式数据
- 使用JSON格式传递数据,获取JSON的值
- SpringMVC学习笔记(7) 返回Json格式数据