JSON × Ajax
//タイムライン読み込み function loadTimeline(){ $.ajax({ url: '<c:url value="/tweetTimeline.do"/>', data: {}, contentType: "application/x-www-form-urlencoded; charset=UTF-8", success: function(data){ $('#timeline-list').empty(); $.each(data, function(i, v){ var li = $("<li style='border-color:#00bf00'></li>"); var div = $("<div></div>"); var anc = $("<a style='font-size:20px;'><b></b></a>"); var anc2 = $("<a></a>"); var img = $("<img style='width:50; height:50px;'></img>"); img.attr('src','<c:url value="/rscsvr/img.irc?img='+ v.userid + '.png"/>'); var icon = $("<i class='icon-star' style='width:200px, height:200px;'></i>"); li.append(img); li.append(anc); li.append(anc2); li.append(div); anc.attr('href', userPagePath + "?userid=" + v.userid); anc.html(v.userid); if((v.liked == '0') && (loginuser != v.userid)){ anc2.attr('href', favPath + "?tweetid=" + v.tweetid); anc2.html(icon); } div.html( autoLink(v.tweet)+'<br>' + "<i class='icon-time' style='width:50px, height:50px;'></i>"+'<font size=2px>'+ v.time + '</font>'+'<br><br>'); $('#timeline-list').append(li); }); }, dataType: 'json' }); } //ツイート投稿 function tweet(){ var msg = $('#tweet_msg').val() ; $.post( '<c:url value="/tweet.do"/>', {tweet: msg}, function( data ) { loadTimeline(); $('#tweet_msg').val(""); $("#notice_dialog").dialog("open"); var ul = $('#timeline-list'); setTimeout(function(){ $('li:first', ul).effect('highlight', 5000); }, 500); }); } //最初の読み込み $(function() { currentTab == 'timeline'; loadTimeline(); //通知ダイアログの初期化 $("#notice_dialog").dialog({ height : 100, position : [ 500, 300 ], autoOpen : false, show : "bounce", hide : "explode" }); //1秒おきにロード setInterval(loadTimeline, 1000); });
JAVAは
/** * ツイートタイムラインアクション * */ public class TimelineAction extends Action { @Autowired private TweetService tweetService; @Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { String userid; PrintWriter out = ResponseUtils.getPrintWriter(response); String paramId = request.getParameter("userid"); if (paramId != null) { userid = paramId; } else { UserDto user = AppUtils.getUser(request); userid = user.getUserid(); } // 自分とフォローのツイート一覧を取得 List<TweetDto> tweets = tweetService.getTweetList(userid); out.print(JSON.encode(tweets)); return null; } }
ってな感じでPrintWriterにDTOの型をそのまま書き込んだ
http://thinkit.co.jp/article/70/1
個人的におすすめ★