The task form

Play Framework: Introduction
COMP 353
Summer 2015-16
 Create a new app
 play new todolist
 The play new command creates a new
directory todolist/ and populates it with a series of
files and directories. The most important are:
 app/ contains the application’s core, split between
models, controllers and views directories. This is the
directory where .java source files live.
 conf/ contains all the application’s configuration files,
especially the main application.conf file,
the routes definition files and the messages files used
for internationalization.
 project contains the build scripts. The build system is
based on sbt. But a new play application comes with
a default build script that will just works fine for our
application.
 public/ contains all the publicly available resources,
which includes JavaScript, stylesheets and images
directories
 test/ contains all the application tests. Tests can be
written as JUnit tests.
 Let’s see how the new application can display this
page.
 The main entry point of your application is
the conf/routes file. This file defines all of the
application’s accessible URLs. If you open the
generated routes file you will see this first route
 GET / controllers.Application.index()
 That simply tells Play that when the web server
receives a GET request for the / path, it must call
the controllers.Application.index() method

 Let’s see what the controllers.Application.index
method looks like. Open the
todolist/app/controllers/Application.java source file:
 You see that controllers.Application.index() returns
a Result. All action methods must return a Result,
which represents the HTTP response to send back to
the web browser.
 Here, the action returns an OK response with an
HTML response body. The HTML content is provided
by a template. Play templates are compiled to
standard Java methods, here as
views.html.index.render(String message)
 This template is defined in
the app/views/index.scala.html source file:
 The first line defines the function signature. Here it
takes a single String parameter. Then the template
content mixes HTML (or any text-based language)
with Scala statements. The Scala statements start
with the special @ character.
Development work-flow
 Remove a double quote from the “hello world”
 reload the home page in your browser:
 As you can see, errors are beautifully displayed
directly in your browser.

Preparing the application
 For our todo list application, we need a few actions
and the corresponding URLs. Let’s start by defining
the routes.
 Edit the conf/routes file:
 We create a route to list all tasks, and a couple of
others to handle task creation and deletion. The route
to handle task deletion defines a variable argument id
in the URL path. This value is then passed to the
deleteTask action method.
 Now if your reload in your browser, you will see that
Play cannot compile your routes files:
 This is because the routes reference non-existent
action methods. So let’s add them to
theApplication.java file:
 As you see we use TODO as result in our actions
implementation. Because we don’t want to write the
actions implementation yet, we can use the built-in
TODO result that will return a 501 Not Implemented
response.
 Try to access http://localhost:9000/tasks
 Now the last thing we need to fix before starting the
action implementation is the index action. We want it
to redirect automatically to the tasks list page:
 As you see we use redirect instead of ok to specify
a 303.
Prepare the Task model
 Before continuing the implementation we need to
define what a Task looks like in our application.
Create a class for it in the app/models/Task.java file:
 We have also created a bunch of static methods to
manage Task operations. For now we wrote dummy
implementation for each operation, but later in this
tutorial we will write implementations that will store
the tasks into a relational database.
The application template
 Our simple application will use a single Web page
containing both the tasks list and the task creation
form. Let’s modify the index.scala.html template for
that:
 We changed the template signature to take 2
parameters:
 A list of tasks to display
 A task form
 We also imported helper._ that give us the form
creation helpers, typically the form function that
creates the HTML <form> with filled action and
method attributes, and the inputText function that
creates the HTML input given a form field.
The task form
 A Form object encapsulates an HTML form definition,
including validation constraints. Let’s create a form
for our Task class. Add this to your Application
controller:
 static Form<Task> taskForm = form(Task.class)
 The type of taskForm is then Form<Task> since it is
a form generating a simple Task. You also need to
import play.data.*.
 We can add a constraint to the Task type using JSR303 annotations. Let’s make the label field required:

Rendering the first page
 Now we have all elements needed to display the
application page. Let’s write the tasks action:
 It renders a 200 OK result filled with the HTML
rendered by the index.scala.html template called with
the tasks list and the task form.
 access http://localhost:9000/tasks in your browser:
 https://playframework.com/documentation/2.0.x/Java
TodoList