处理JSON:使用Spring MVC,实现json数据的返回。

处理JSON:使用Spring MVC,实现json数据的返回。

  1. 加入 jar 包:

jackson-annotations-2.1.5.jar

jackson-core-2.1.5.jar

jackson-databind-2.1.5.jar

  1. 编写目标方法,使其返回 JSON 对应的对象或集合

@ResponseBody  //SpringMVCJSON的支持

@RequestMapping("/testJSON")

public Collection<Employee> testJSON(){

return employeeDao.getAll();

}

  1. 编写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>

转载于:https://my.oschina.net/u/4151492/blog/3064451

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值