@ResponseBody將集合數據轉換為json格式並返回給客戶端
阿新 • • 發佈:2017-12-18
-m path ray es2017 cnblogs class framework add work
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格式並返回給客戶端