Book Image

Spring Cookbook

Book Image

Spring Cookbook

Overview of this book

Table of Contents (19 chapters)
Spring Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Retrieving the tweets of a Twitter user


In this recipe, you'll learn how to retrieve the last tweets of a Twitter user from a Spring web application.

Getting ready

This recipe uses the code from the Connecting to Twitter recipe.

How to do it…

Here are the steps to retrieve the last tweets of a Twitter user:

  1. In the TwitterController class, add a Model argument to the tw() method:

    @RequestMapping("/fw")
    public String fb(HttpServletRequest request, Model model) {
    ...
  2. In that method, use the Twitter object to retrieve the user's tweets:

    List<Tweet> tweets = twitter.timelineOperations().getUserTimeline();
  3. Pass the list of tweets to the JSP view:

    model.addAttribute("tweets", tweets);
  4. In the JSP, display the list of tweets:

    <c:forEach items="${tweets}" var="tweet">
      <p>${tweet.text}</p>
    </c:forEach>