處理JSON:使用Spring MVC,實現json數據的返回。javascript
jackson-annotations-2.1.5.jarjava
jackson-core-2.1.5.jarajax
jackson-databind-2.1.5.jarjson
@ResponseBody //SpringMVC對JSON的支持app
@RequestMapping("/testJSON")測試
public Collection<Employee> testJSON(){url
return employeeDao.getAll();spa
}對象
<a href="testJson">測試Json</a>ip
4)如何使用消息轉換
@RequestBody @ResponseBody
HttpEntity ResponseEntity
5)ajax 添加
<script type="text/javascript"> $(function(){ $("#btn").click(function(){ $.ajax({ url:"testJson", type:"POST", dataType:"json", success:function(msg){ $("body").append("<table></table>"); $("table").append("<tr><th>id</th><th>lastName</th><th>email</th><th>gender</th><th>departmentName</th></tr>"); for(var i in msg){ var emp=msg[i]; $("table").append("<tr><td>"+emp.id+"</td><td>"+emp.lastName+"</td><td>"+emp.email+"</td><td>"+emp.gander+"</td><td>"+emp.department.departmentName+"</td></tr>") } } }); }); }); </script>