spring-mvc.xml:
<beans ????xmlns:mvc="http://www.springframework.org/schema/mvc" ???><mvc:annotation-driven/>
js代码:
$.post("${pageContext.request.contextPath}/getJson",{},function(data){ ????????????????????alert(JSON.stringify(data));});
java代码:
@RequestMapping("/getJson")@ResponseBodypublic List<User> getJson(){ ???List<User> list = new ArrayList<User>(); ???User user1 = new User(10, "刘德华", 45); ???User user2 = new User(12, "张学友", 46); ???list.add(user1); ???list.add(user2); ???return list;}
导入jackson的Jar包
@ResponseBody将集合数据转换为json格式并返回给客户端
原文地址:http://www.cnblogs.com/lxcmyf/p/8057743.html