Five cool things you can do with Play!

Five examples to show the philosophy behind the Play! framework.

1. Bind an HTTP parameter to a Java method parameter

Retrieving HTTP parameters from Java code is really simple with Play!. Just declare method parameters with the same name as the HTTP parameters.

For example, with this request :

/articles/archive?date=08/01/08&page=2

You can retrieve the date and page parameters by declaring them as Java method parameters :

public static void archive(Date date, Integer page) {
    List<Article> articles = Articles.fromArchive(date, page);
    render(articles);
}

Smart binding also functions with any classes.

public class Person {
  String name;
  Integer age;
}

A simplistic controller action to add a person may looks like this :

public static void add(Person p) {
  p.save();
}

The HTML form defines fields with composite names :

<form action="/Directory/add" >
 Name: <input type="text" name="p.name" />
 Age: <input type="text" name="p.age" />
</form>

More about quick persistence

2. Redirect to an action by simply calling the corresponding Java method

There is no equivalent to the Java Servlet forward command with Play!. But redirecting to another action is really simple. Just call the corresponding Java method and Play! will generate the correct HTTP Redirect response.

public static void show(Long id) {
    Article article = Article.findById(id);
    render(article);
}

public static void edit(Long id, String title) {
    Article article = Article.findById(id);
    article.title = title;
    article.save();
    show(id);
}

Note how at the end of the edit action, we redirect to the show action.

In any template you can use the equivalent syntax to generate the link :

<a href="@{Article.show(article.id)}">${article.title}</a>

That will generate HTML like :

<a href="/articles/15">My new article</a>

3. Don't Repeat Yourself when passing Java objects to templates

In most Java frameworks, to pass some Java objects to the template system you need to write something like :

Article article = Article.findById(id);
User user = User.getConnected();
Map<String, Object> model = new HashMap<String,Object>();
model.put("article", article);
model.put("user", user);
render(model);

When, with Play you can just write :

Article article = Article.findById(id);
User user = User.getConnected();
render(article, user);

That saves a lot of useless lines of code …

4. Straightforward file upload management

File upload management is so simple with Play!.

The HTML form :

<form action="@{Article.uploadPhoto}" method="POST" enctype="multipart/form-data">
    <input type="text" name="title" />
    <input type="file" id="photo" name="photo" />
    <input type="submit" value="Send it ..." />
</form>

And the Java code :

public static void uploadPhoto(String title, File photo) {
   ...
}

How could it be easier ?

5. Distribute an application to several JVMs without configuring anything

Since the server is totally stateless, you can just start the same application on several servers and load balance between applications at the HTTP level. No need to set up a complex application server cluster or something similar.

Page infos : contents/fivecoolthings.txt · Last modified: 2009/03/18 16:30 by tracy
Show pagesource - Old revisions - Backlinks -