应用jQuery技术读取经过MappingJacksonJsonView转化的集合对象
- - ITeye论坛最新讨论经过Spring MVC MappingJacksonJsonView转化后返回的JSON数据:. Spring MVC控制器处理来自“test/getJSON”的请求:. strResult += "
经过Spring MVC MappingJacksonJsonView转化后返回的JSON数据:
{ "users": [ { "userid": null, "username": "user_01", "pass": "123" }, { "userid": null, "username": "user_02", "pass": "456" } ] }
Spring MVC控制器处理来自“test/getJSON”的请求:
@RequestMapping("/getJSON") public ModelAndView getJSON() { System.out.println("getJSON"); ModelAndView mav = new ModelAndView(); List<User> users = this.getUsers(); mav.addObject("users", users); mav.setViewName("testJSON"); return mav; }
在页面读取返回数据:
<script type="text/javascript"> $(function(){ $("#testJson").click(function(){ $.ajax({ type:"GET", url:"http://localhost:8080/SpringTest/test/getJSON?content=json", dataType:"json", contentType:'application/json;charset=UTF-8', success:function(data){ $("#result").empty(); var strResult = ""; var users = data.users; for(index in users){ strResult += "<div class='comment'>用户名:" + users[index].username + "<br />密码:" + users[index].pass + "<hr /></div>"; } $("#result").html(strResult); }, error:function(){ alert("error."); } }); }); }) </script>
在配置$.ajax()的url时,我写的url明显是冗长的。但是目前我还没有找到一个正确的、合理的处理方式。
密码:" + users[index].pass + "