Play framework

Play makes web development easy

I’ve been looking quite a bit at the play framework lately, after receiving a tip from Arild, one of my colleagues at Miles. I’ve been amazed by how easy it is to build web applications with the framework. You can really accomplish a whole lot with very little code, a very attractive feature indeed. Fewer lines of code means fewer bugs and fewer bugs means less maintenance and more time to deliver new stuff. A positive feedback loop.


Now, you can judge for yourselves, have a look at the framework and see what you think. The documentation is very good indeed. So are the tutorials. I would definitely recommend going through the Yet Another Blog Engine tutorial. It walks you through a lot of the existing functionality in the framework, like JPA support through the Model abstraction, CRUD, security, testing etc.

Showing twitter updates on the blog

Having implemented the YABE application by following the tutorial I wanted to create some screens on my own, and decided that I would try to integrate a twitter timeline update into the blog engine. There are lots of different Twitter APIs out there. I chose to use the Twitter4J. Adding it to the classpath was simply a matter of:

  • Add the Twitter4J jar file to the lib folder of the application (I used the 2.0.9 version)
  • Run the command play eclipsify so that Eclipse could pick up the new library

With this in place I was ready to go. First thing to do was to create a Model object. The goal was to show my own updates on the right side of my new blog engine. So I figured that creating an Update object to hold a tweet:

package models;

public class Update {
	private TwitterAdapter twitterAdapter;
	public String text;
	public Date createdAt;

    public static List getUpdates() {
    	return twitterAdapter.getUpdates();
    }	
}

public class TwitterAdapter{
   
   public List<Update> getUpdates(){
	   Twitter twitter = new Twitter("ketilj", "secret"); // login to
	   statuses = mapStatuses(twitter.getUserTimeline(), numberOfUpdates);
   }

   private static List mapStatuses(List updates) {
	   List statuses = new ArrayList();
	   Iterator ite = updates.iterator();
	   while (ite.hasNext()) {
		   Status status = (Status) ite.next();	
		   Update s = new Update();
		   s.text = status.getText();
		   status.getCreatedAt();
		   statuses.add(s);
	   }
	   return statuses;
   }
}

Next thing to do is to create a controller method that gets the Update objects and renders it. I simply put this in the index method of the Application controller class. This method is called when the front page is loaded.

public class Application extends Controller {

    public static void index() {
         List<Update> updates = Update.getUpdates();
         render(updates);
    }
    ...

The call to render will exexture the app/views/Application/index.html template.
So, all we need to do display the tweets on the front page is to add a List tag to the app/views/Application/index.html page, and configure the style sheet to display it on the right side of the web page:

<div id="sidebar">
<h3>Updates from twitter</h3>
	<p>#{list items:updates, as:'update'} 
			${update.text}
		#{/list}
	</p>
</div>

That’s it. My twitter updates are now displayed on the right side of the blog.

Testing the application

Testing in Play is a breeze. You may of course run your tests in your favorite IDE, or you can run the application in test mode and have a nice web interface showing all your unit tests and Selenium tests. The Selenium support in the framework is really good, creating these kind of tests are really easy. You can read more about it here.

Conclusion

Again I was amazed by the simplicity of doing web development in the Play framework, even for a guy who has spent the last 6 or 7 years on programming backend stuff. Of course, my knowledge of CSS is crap, so the twitter update stuff didn’t get the look and feel that I wanted. Hopefully, I can find some time in the future to teach myself some CSS styling stuff and start to build some cool web sites with Play.

Advertisements

3 thoughts on “Play makes web development easy

  1. Yes, you’re right. I chose this framework to build a project from the start at my work and i’m loving it, specially that does everything “out-of-box” without having to configure hundreds of xmls.

    The great advantage of using this framework is that you can change your database “on-runtime”.

    See ya. Good post.

  2. Hi! yes it’s so easy. But I prefer do it much easier like:

    package controllers;

    import java.util.List;

    import play.mvc.Controller;
    import twitter4j.Status;
    import twitter4j.Twitter;
    import twitter4j.TwitterException;
    import twitter4j.TwitterFactory;

    public class TwitterStatus extends Controller {

    public static void index() throws TwitterException{
    Twitter twitter = new TwitterFactory().getInstance(“user”, “pass”);
    List tweetsHomeTimeline = twitter.getHomeTimeline();
    render(tweetsHomeTimeline);
    }
    }

    and
    views/TwitterStatus/index.html

    #{list items:tweetsHomeTimeline, as:’status’ }
    ${status.user.screenName}: ${status.text}
    #{/list}

    See you! Good Post

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s