处理JSON:使用Spring MVC,实现json数据的返回。
- 加入 jar 包:
jackson-annotations-2.1.5.jar
jackson-core-2.1.5.jar
jackson-databind-2.1.5.jar
- 编写目标方法,使其返回 JSON 对应的对象或集合
@ResponseBody //SpringMVC对JSON的支持
@RequestMapping("/testJSON")
public Collection<Employee> testJSON(){
return employeeDao.getAll();
}
- 编写index页面
<a href="testJson">测试Json</a>
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>