1、导入jar包
jack-core-asl-1.9.11.jar
jack-mapper-asl-1.9.11.jar
2、配置springmvc-servlet.xml文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <context:component-scan base-package="com.wh"></context:component-scan> <mvc:annotation-driven ></mvc:annotation-driven> <!-- 配置解析JSON类型 --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" /> </list> </property> </bean> </beans>
3、编写javascript的ajax和后台springmvc的控制器
handler.java
package com.wh.handler; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class TestResponseBody { /** * 返回一个字符串的json格式 * * produces="text/html;charset=UTF-8" * 声明返回的类型 */ @RequestMapping(value="/dealAjax.action",produces="text/html;charset=UTF-8") public @ResponseBody String dealAjax(){ return "成功"; } /* <script type="text/javascript" src="${pageContext.request.contextPath}/bootstrap/js/jquery.js"></script> <script> $(function() { $('button').click(function() { $.ajax({ "url" : "dealAjax.action", "type" : "post" "data":{"username":"jack"} }).done(function(result) { alert(result); }).fail(function(e) { alert(e); }); }); }); </script> */ /** * 返回一个对象的json格式 * * 传对象时,不能指定为produces="text/html;charset=UTF-8" */ @RequestMapping(value="/dealAjax2.action") public @ResponseBody User dealAjax2(){ User u=new User("张三",20,"男"); return u; } /* <script> $(function() { $('button').click(function() { $.ajax({ "url" : "${pageContext.request.contextPath}/dealAjax2.action", "type" : "post", //注意:这里不能加下面这行,否则数据会传不到后台 // "contentType": "application/json;charset=utf-8", "data":{"username":"jack"} }).done(function(data) { console.log("success..."); console.log(data); alert(data.uname+"----"+data.uage+"----"+data.usex); //alert(data.uname); }).fail(function(e) { console.log("error..."); console.log(e.responseText); //alert(e); }); }); }); </script> */ /** * 返回一个list集合的json格式 * * 传对象时,不能指定为produces="text/html;charset=UTF-8" */ @RequestMapping(value="/dealAjax3.action") public @ResponseBody List<User> dealAjax3(){ List<User> list=new ArrayList<User>(); User u1=new User("张三",20,"男"); User u2=new User("张三",20,"男"); User u3=new User("张三",20,"男"); list.add(u1); list.add(u2); list.add(u3); return list; } /* <script> $(function() { $('button').click(function() { $.ajax({ "url" : "${pageContext.request.contextPath}/dealAjax3.action", "type" : "post", //注意:这里不能加下面这行,否则数据会传不到后台 //"contentType": "application/json;charset=utf-8", "data":{"username":"jack"} }).done(function(data) { $.each(data,function(i,item){ console.log("i="+i); console.log("item"+item); console.log(item.uname+"---"+item.uage); }); }).fail(function(e) { console.log("error..."); console.log(e.responseText); }); }); }); </script> */ /** * 返回一个Map集合的json格式,且map中的key值写死了 */ @RequestMapping(value="/dealAjax4.action") @ResponseBody //此注解不能省略 否则ajax无法接受返回值 public Map<String,User> dealAjax4(){ Map<String,User> map=new HashMap<String,User>(); map.put("map1",new User("张三",20,"男")); return map; } /* * <script> $(function() { $('button').click(function() { $.ajax({ "url" : "${pageContext.request.contextPath}/dealAjax4.action", "type" : "post", "dataType":"json", "data":{"username":"jack"} }).done(function(data) { alert(data.map1.uname+"-----"+data.map1.uage); console.log(data.map1+data.map1.uname+"-----"+data.map1.uage); }).fail(function(e) { console.log("error..."); console.log(e.responseText); }); }); }); </script> */ /** * 返回多个Map集合的json格式 * 并在前台遍历输出 * for ( var key in data) { * console.log(key+"---"+data[key].uname+"---"+data[key].uage); * } */ @RequestMapping(value="/dealAjax5.action") public @ResponseBody Map<String,User> dealAjax5(){ Map<String,User> map=new HashMap<String,User>(); map.put("map1",new User("张三",20,"男")); map.put("map2",new User("李四",21,"男")); map.put("map3",new User("王武",22,"男")); return map; } /* <script> $(function() { $('button').click(function() { $.ajax({ "url" : "${pageContext.request.contextPath}/dealAjax5.action", "type" : "post", "dataType":"json", "data":{"username":"jack"} }).done(function(data) { for ( var key in data) { console.log(key+"---"+data[key].uname+"---"+data[key].uage); } }).fail(function(e) { console.log("error..."); console.log(e.responseText); }); }); }); </script> */ }
遍历map集合的参考地址:http://blog.163.com/xueling1231989@126/blog/static/102640807201461744258436/