Presentation is loading. Please wait.

Presentation is loading. Please wait.

Play Framework: Introduction

Similar presentations


Presentation on theme: "Play Framework: Introduction"— Presentation transcript:

1 Play Framework: Introduction
COMP 353 Summer

2 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:

3 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.

4 project contains the build scripts. The build system is based on sbt
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.

5 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

6 Let’s see what the controllers. Application. index method looks like
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.

7 Here, the action returns an OK response with an HTML response body
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:

8 The first line defines the function signature
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 character.

9 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.

10 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:

11 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:

12

13 As you see we use TODO as result in our actions implementation
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

14 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.

15 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:

16 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.

17 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:

18

19 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.

20 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 JSR-303 annotations. Let’s make the label field required:

21

22 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  your browser:

23 https://playframework.com/documentation/2.0.x/JavaTodoList

24


Download ppt "Play Framework: Introduction"

Similar presentations


Ads by Google