Book Image

Play Framework essentials

By : Julien Richard-Foy
Book Image

Play Framework essentials

By: Julien Richard-Foy

Overview of this book

Table of Contents (14 chapters)
Play Framework Essentials
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Generating HTML forms


The two routes of our application using the GET verb (Items.list and Items.details) now return an HTML page. However, the remaining routes are not currently reachable by our web users. Web browsers can perform POST requests only when an HTML form is submitted or if some client-side code sends an XmlHTTPRequest.

Let's add an HTML page that contains a form to create new items, which will look like the following:

First, we need to define an HTTP endpoint for the page containing the form and then write the according route:

GET     /items/add     controllers.Items.createForm

Note

Note that as routes are tried in their definition order, this route must be defined before the Items.details one, as their URL pattern overlap.

Then, we define the corresponding action in the Items controller:

val createForm = Action {
  Ok(views.html.createForm())
}

The Java equivalent code is as follows:

public static Result createForm() {
    return ok(views.html.createForm.render());
}

This action just...