Play Framework: Introduction

Slides:



Advertisements
Similar presentations
HTML Forms. collect information for passing to server- side processes built up from standard widgets –text-input, radio buttons, check boxes, option lists,
Advertisements

Presenter: James Huang Date: Sept. 29,  HTTP and WWW  Bottle Web Framework  Request Routing  Sending Static Files  Handling HTML  HTTP Errors.
JavaScript FaaDoOEngineers.com FaaDoOEngineers.com.
The Librarian Web Page Carol Wolf CS396X. Create new controller  To create a new controller that can manage more than just books, type ruby script/generate.
Lecture 6/2/12. Forms and PHP The PHP $_GET and $_POST variables are used to retrieve information from forms, like user input When dealing with HTML forms.
Introduction to MVC Adding a View Page NTPCUG Tom Perkins, Ph.D.
Ruby on Rails Model of MVC. Model-View-Controller Paradigm A way of organizing a software system Benefits: Isolation of business logic from the user interface.
The Web Warrior Guide to Web Design Technologies
1 Chapter 12 Working With Access 2000 on the Internet.
Python and Web Programming
Introduction to scripting
Christopher M. Pascucci Basic Structural Concepts of.NET Browser – Server Interaction.
Forms, Validation Week 7 INFM 603. Announcements Try placing today’s example in htdocs (XAMPP). This will allow you to execute examples that rely on PHP.
Form Handling, Validation and Functions. Form Handling Forms are a graphical user interfaces (GUIs) that enables the interaction between users and servers.
Javascript and the Web Whys and Hows of Javascript.
M. Taimoor Khan * Java Server Pages (JSP) is a server-side programming technology that enables the creation of dynamic,
Reading Data in Web Pages tMyn1 Reading Data in Web Pages A very common application of PHP is to have an HTML form gather information from a website's.
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
Standalone Java Application vs. Java Web Application
Introduction to ASP.NET MVC Information for this presentation was taken from Pluralsight Building Applications with ASP.NET MVC 4.
Chapter 8 Cookies And Security JavaScript, Third Edition.
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
Mr. Justin “JET” Turner CSCI 3000 – Fall 2015 CRN Section A – TR 9:30-10:45 CRN – Section B – TR 5:30-6:45.
Chapter 8 Collecting Data with Forms. Chapter 8 Lessons Introduction 1.Plan and create a form 2.Edit and format a form 3.Work with form objects 4.Test.
Website Development with PHP and MySQL Saving Data.
Introduction to JavaServer Pages. 2 JSP and Servlet Limitations of servlet  It’s inaccessible to non-programmers JSP is a complement to servlet  focuses.
Chapter 6 Server-side Programming: Java Servlets
1 © Netskills Quality Internet Training, University of Newcastle HTML Forms © Netskills, Quality Internet Training, University of Newcastle Netskills is.
Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.
Introduction.  The scripting language most often used for client-side web development.  Influenced by many programming languages, easier for nonprogrammers.
Rails & Ajax Module 5. Introduction to Rails Overview of Rails Rails is Ruby based “A development framework for Web-based applications” Rails uses the.
XP Tutorial 8 Adding Interactivity with ActionScript.
Introduction into JavaScript Java 1 JavaScript JavaScript programs run from within an HTML document The statements that make up a program in an HTML.
©SoftMooreSlide 1 Introduction to HTML: Forms ©SoftMooreSlide 2 Forms Forms provide a simple mechanism for collecting user data and submitting it to.
RESTful Web Services What is RESTful?
(Some from Chapter 11.9 – “Web” 4 th edition and
AJAX. Overview of Ajax Ajax is not an API or a programming language Ajax aims to provide more responsive web applications In normal request/response HTTP.
INTRODUCTION JavaScript can make websites more interactive, interesting, and user-friendly.
JavaScript Introduction and Background. 2 Web languages Three formal languages HTML JavaScript CSS Three different tasks Document description Client-side.
Java Server Pages. 2 Servlets The purpose of a servlet is to create a Web page in response to a client request Servlets are written in Java, with a little.
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
1 CSC160 Chapter 1: Introduction to JavaScript Chapter 2: Placing JavaScript in an HTML File.
BIT 286: Web Applications Lecture 04 : Thursday, January 15, 2015 ASP.Net MVC -
Lesson 5 Introduction to HTML Forms. Lesson 5 Forms A form is an area that can contain form elements. Form elements are elements that allow the user to.
CS520 Web Programming Spring – Web MVC Chengyu Sun California State University, Los Angeles.
1 Using MVC 6. MVC vs. ASP Web Forms Both run under ASP.NET Can coexist In Web Forms, browser requests page. xxx.aspx and xxx.aspx.cs on the server Page.
Chapter 1 Getting Started with ASP.NET Objectives Why ASP? To get familiar with our IDE (Integrated Development Environment ), Visual Studio. Understand.
Jim Fawcett CSE686 – Internet Programming Summer 2010
Jim Fawcett CSE686 – Internet Programming Spring 2014
Module 1 Introduction to JavaScript
Chapter 5 Validating Form Data with JavaScript
Jim Fawcett CSE686 – Internet Programming Spring 2012
Tutorial 10 Programming with JavaScript
z/Ware 2.0 Technical Overview
Play Framework: Introduction
Introduction to Programming the WWW I
Arrays and files BIS1523 – Lecture 15.
Chapter 27 WWW and HTTP.
PHP.
T. Jumana Abu Shmais – AOU - Riyadh
Chapter 15 Introduction to Rails.
HTTP GET vs POST SE-2840 Dr. Mark L. Hornick.
Lecture 5: Functions and Parameters
Intro to PHP.
MVC Controllers.
Java Script Siddharth Srivastava.
Mr. Justin “JET” Turner CSCI 3000 – Fall 2016 Section DA MW 4:05-5:20
Introduction to JavaScript
Web Forms.
Presentation transcript:

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 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 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 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 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 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 JSR-303 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/JavaTodoList