1. 导入 json 包:
jackson-annotations-2.8.9.jar jackson-core-2.8.9.jar jackson-databind-2.8.9.jar json.jar
2. js 代码
$.ajax({ url: "url", type: "post/get", data: { key1:val1, key2:val2, }, dateType:'json', success: function(data){ /*将旧列表内容移除*/ $("#comment_list li").remove(); if( data!=null ){ for(var i=0;i){ /*循环更新列表内容*/ $("#comment_list").append( "" + " #" + (data['comment'].length-i) + "" + " ) } document.getElementById("comment-textarea").value = ""; } } })
3. controller 代码
@RequestMapping(value="/url") @ResponseBody public MapcommentAdd(String acount,Integer articleId,String content,String time,Integer editor){ Map map = new HashMap (); List comments = null; time = new TimeFormat().timeFormat(new Date()); int r = commentService.commentAdd(acount, articleId, content, time,null); if(r != 0 ){ comments = commentService.getCommentsByArticleId(articleId,null); map.put("comment", comments); } System.out.println("==>"+acount+articleId+content+time); return map; }
" + data['comment'][i]['content'] + "" + "