springmvc ajax

来源:互联网 发布:金十数据官方网站 编辑:程序博客网 时间:2024/05/18 01:46

1. 接收字符串:

jsp:

$.ajax({url : "${pageContext.request.contextPath}/testRequestBody",// 发送请求的URL字符串。dataType : "html", // 预期服务器返回的数据类型。   type : "post", //  请求方式 POST或GET //  contentType:"", //  发送信息至服务器时的内容编码类型   // 发送到服务器的数据。   data:"id=1",   async:  true , // 默认设置下,所有请求均为异步请求。如果设置为false,则发送同步请求   // 请求成功后的回调函数。   success :function(data){   $("#id").append(data);   },   // 请求出错时调用的函数   error:function(){   alert("数据发送失败");   }


后台contorller

public void setJson(String id,HttpServletResponse response) throws Exception{        System.out.println(id);    response.setContentType("text/html;charset=UTF-8");    Integer id1 = Integer.parseInt(id);    response.getWriter().println(id1);    }


2 json 数据的接收

2.1  使用jackson  的 jar


jsp:

$.ajax({url:"${pageContext.request.contextPath}/json", // 发送请求的URL字符串。dataType : "json",    type : "post",    contentType:"application/json", //  发送信息至服务器时的内容编码类型   // 发送到服务器的数据。  data:JSON.stringify({id : 1, name : "我是json数据"}),   async:  true , // 默认设置下,所有请求均为异步请求。如果设置为false,则发送同步请求   // 请求成功后的回调函数。   success :function(data){   alert(date);   $.each(data,function(){ //遍历   var tr  = $("<tr align='center'/>");   $("<td/>").html(this.id).appendTo(tr);   $("<td/>").html(this.name).appendTo(tr);   $("<td/>").html(this.author).appendTo(tr);  $("#booktable").append(tr);   });
},   // 请求出错时调用的函数   error:function(){   alert("数据发送失败");   }});

<body>

<table id="booktable" border="1" style="border-collapse: collapse;">

    <tr align="center">    <th>编号</th>    <th>作者</th>    <th>作者</th>    </tr>

</table>

后台:controller:

@RequestMapping(value="/json")    public void setJson(@RequestBody Book book,    HttpServletResponse response) throws Exception{    // ObjectMapper类是Jackson库的主要类。它提供一些功能将Java对象转换成对应的JSON格式的数据    ObjectMapper mapper = new ObjectMapper();    // 将book对象转换成json输出    book.setAuthor("zhangsan");    response.setContentType("text/html;charset=UTF-8");    // 将book对象转换成json写出到客户端    response.getWriter().println(mapper.writeValueAsString(book));    }
book 实体类省略


2.2

jasjson

jar


jsp.

$.ajax({url:"${pageContext.request.contextPath}/json/testRequestBody", // 发送请求的URL字符串。dataType : "json", // 预期服务器返回的数据类型。   type : "post", //  请求方式 POST或GET   contentType:"application/json", //  发送信息至服务器时的内容编码类型   // 发送到服务器的数据。//   data:JSON.stringify({id : 1, name : "Spring MVC企业应用实战"}),   async:  true , // 默认设置下,所有请求均为异步请求。如果设置为false,则发送同步请求   // 请求成功后的回调函数。   success :function(data){   alert(date);   $.each(data,function(){ //遍历   var tr  = $("<tr align='center'/>");   $("<td/>").html(this.id).appendTo(tr);   $("<td/>").html(this.name).appendTo(tr);   $("<td/>").html(this.author).appendTo(tr);  $("#booktable").append(tr);   });    },   // 请求出错时调用的函数   error:function(){   alert("数据发送失败");   }});

controller:

@RequestMapping("/json")@ResponseBody  //将集合数据转换json格式并将其返回客户端public Object getJson(){List<Book> list = new ArrayList<Book>();list.add(new Book(1,"spring mvc","张三"));list.add(new Book(2,"java ee","李四"));return list;}

// @RequestBody根据json数据,转换成对应的Object    @RequestMapping(value="/json")    public void setJson(@RequestBody Book book,    HttpServletResponse response) throws Exception{      book.setAuthor("张三");    response.setContentType("text/html;charset=UTF-8");    // 将book对象转换成json写出到客户端    response.getWriter().println(JSONObject.toJSONString(book));    }



原创粉丝点击