Overview
- A Java/Scala web application framework which aims to improve the productivity (development speed) and flexibility (easiness of change).
- Although Applications can be coded with both Java and Scala, templates must be written with Scala.
- Its design is affected by Ruby on Rails such as Convention over Configuration or MVC.
How to Install
- Install JDK (6 or later)
- Set two environment variable: JAVA and JAVA_HOME.
- Download Play Framework on the following website.
- Call commands in the command prompt.
Frequently Used Commands
Command |
Description |
activator new [app name] play-java |
Create a new project |
run [port number] |
Run an application |
compile |
Compile the source codes(Usually compile is done when accessing the application) |
eclipse |
Covert the project to an Eclipse project |
How to Import to Eclipse
- Convert the project to an Eclipse project by using eclipse command.
- Open Eclipse and select "Import" and import the project as a "Existing project".
- Turn off the feature which automatically deletes import statements.
- Turn off "Project property -> Java -> Editor -> Save Actions -> Organize imports"
- Add a class folder for the view of Play framework.
- Project Property -> Java Build Path -> Add Class Folder
- Add target/target/scala-2.11/classes_managed
- (Appendix) Resolve the dependency from Maven repository using SBT
- SBT (Simple Build Tool) is a build tool which is recommended in Play Framework.
- Look for necessary libraries in Maven repository.
- Copy the text in the SBT tab, and paste it in build.scala.
- Re-compile the project.
libraryDependencies ++= Seq(
javaJdbc,
...,
"com.nimbusds" % "oauth2-oidc-sdk" % "4.13"
)
$ clean
$ compile
$ eclipse with-source=true
# Refresh the Eclipse project
Create a view
- Create a template (*.scala.html) in app/views directory.
=> After compiling a class named views.html.{view name} will be created.
- Available Control Syntax
- if / for / function / variable / import
- Helper methods
- Specify other endpoints as links or targets of forms.
- @routes.{controller class name}.{method name}
DB access using Ebean (O/R mapper)
db.default.driver = {JDBC Driver}
db.default.url = {URL starting with jdbc:}
db.default.driver = org.h2.Driver
# Change "mem" to "file" when we want to make the data persistent.
db.default.url = jdbc:h2:mem:play
- Ebean class
- Package name: models.{class name} (customizable)
- Extends play.db.ebean.Model
- Available annotations
Annotation |
Description |
@Entity |
Specify to an Ebean class. |
@Id |
Specify to a member which is a primary key. |
@NotNull |
Specify to a member which uses a NotNull constraint. |
@CreatedTimestamp |
Specify to a member which stores a created date and time. |
@Version |
Specify to a member which stores a updated date and time. |
Operation |
Method |
INSERT |
save() |
UPDATE |
update() |
DELETE |
delete() |
Method |
Description |
all() |
Search whole the records. |
byId(Long id) |
Search by a primary key. |
where(String arg) |
Search with a condition. |
- Relations between tables 1:N (parent-child relationship)
@Entity public class Child extends Model {
@Id public Long id;
@ManyToOne
@JoinColmunName(name=”parent_id”)
public Parent parent;
...
}
@Entity publi class Parent extends Model {
@Id public Long id;
@OneToMany(cascade=CascadeType.ALL, mappedBy=”parent”)
public List<Child> children = new ArrayList<Child>();
}
- Transactioins: Use TxRunnable.
Ebean.execute(new TxRunnable() {
// Write Ebean procedures in the transaction.
});
How to use a controller
- Write a routing configration in conf/routes.
{HTTP Method} {Path} {Method}
# Simple example
GET /list controllers.Page.showList()
# Pass the Path parameter and the query parameters
GET /search/:id controllers.Page.search(id :Integer, name :String)
- Methods in a Controller class
- Create a method which returns play.mvc.Result.
- A helper method is prepared in play.mvc.Results.
- ok, redirect, badRequest, internalServerError, …
Session Scope and Flash Scope
- Session Scope
- Retained in a user session
- Can store up to 4KB string
- How it works
- Stored in the client side using HTTP Cookie.
- The values of the cookie are signed with the secret key. (The values will be invalidated.)
- There is no expiration date for the session. (As long as the client break it.)
-> Store a timestamp in the session and manage the expiration date.
- Code example
# Save values into the session
session(“key”, “value”);
# Read values from the session
String value = session(“key”);
# Delete values from the session
session.remove(“key”);
# Delete the session
session().clear();
- Flash Scope (The difference from the session)
- The data will be retained until the next request.
- The values of the cookie are not signed. (They may be changed by a user.)
→ 非Ajaxアプリケーションでのメッセージ(成功/失敗など)のやり取りに利用する。
Refer a conf file
- an instance of application.conf can be gotten with Configration#root.
Configuration.root().getString("application.langs")
How to use Bootstrap
- WebJars enables us to install Bootstrap via SBT.