h1. Five cool things you can do with Play

Five examples to show the philosophy behind the Play framework.

h2. <a>Bind an HTTP parameter to a Java method parameter</a>

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

For example, with this request:

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

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

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

Play will use the method parameter’s static type, to translate the HTTP value into a Java object.

Smart binding also works **with any class.**

bc. public class Person {
    public String name;
    public Integer age;
}

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

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

The HTML form defines fields with composite names:

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


h2. <a>Redirect to an action by calling the corresponding Java method</a>

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.

bc. public static void show(Long id) {
    Article article = Article.findById(id);
    render(article);
}
 
bc. 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:

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

That will generate the following HTML:

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


h2. <a>Don’t Repeat Yourself when passing Java objects to templates</a>

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

bc. 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);

With Play, you can just write:

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

And retrieve objects from their Java local name in the template. That saves a lot of useless lines of code…

h2. <a>JPA on steroids</a>

JPA is surely the best object-relational mapping (ORM) API available for Java. If you know it you will be amazed how much simpler it becomes with Play. With nothing to configure, Play will automatically start the JPA Entity Manager using Hibernate and magically synchronize it when code is reloaded.

Moreover, if you use the provided @play.db.jpa.Model@ superclass it will help to make your code prettier. Take a look:

bc. public void messages(int page) {
    User connectedUser = User.find("byEmail", connected()).first();
    List<Message> messages = Message.find(
        "user = ? and read = false order by date desc",
        connectedUser
    ).from(page * 10).fetch(10);
    render(connectedUser, messages);
}

h2. <a>Straightforward file upload management</a>

File upload management is very simple with Play.

The HTML form:

bc. #{form @uploadPhoto(), enctype:'multipart/form-data'}
    <input type="text" name="title" />
    <input type="file" id="photo" name="photo" />
    <input type="submit" value="Send it..." />
#{/}

And the Java code:

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

How could it be easier?
