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)); }
阅读全文
0 0
- springmvc ajax
- SpringMVC+AJAX
- Springmvc+ajax
- springmvc ajax
- springmvc ajax
- springmvc ajax
- springmvc ajax
- 【SpringMVC】Ajax
- [spring3]springMVC 注解ajax
- SpringMVC AJAX支持
- SpringMvc 中使用ajax
- springMVC+ajax分页查询
- jquery+ajax+springmvc
- 2.ajax+springmvc+json
- Ajax与SpringMvc交互
- SpringMvc + ajax 文件上传
- SpringMVC Ajax请求
- SpringMVC处理AJAX请求
- Windows 权限管理
- opengl实现线性雾
- hiveSql的一些问题
- Spring学习系列: 环境搭建(vsftpd)
- 智能配送与货物跟踪图
- springmvc ajax
- Android Studio---删除编译器中缩进提示线
- Build Path
- Java中守护线程
- react集成 VS 继承
- 性能测试体系的知识分享
- Java 移位操作符注意
- 287. Find the Duplicate Number 寻找重复数
- Linux技巧: Linux终端打印如何设置不同的颜色及显示方式