Play is a highproductivity Java and Scala web application framework that integrates the components and APIs you need for modern web application development. Play is based on a lightweight, stateless, webfriendly architecture and features predictable and minimal resource consumption (CPU, memory, threads) for highlyscalable applications thanks to its reactive model, based on Akka Streams.
Play framework VinaSaver – Tien Nguyen 2017 Play framework - Overview - Play is a high-productivity Java and Scala web application framework that integrates the components and APIs you need for modern web application development - Play is based on a lightweight, stateless, web-friendly architecture and features predictable and minimal resource consumption (CPU, memory, threads) for highly-scalable applications thanks to its reactive model, based on Akka Streams Play Framework ADVANtages Play Framework Like many frameworks, Play is based on the MVC model (model, view, controller) Runs on the JVM, which has years of experience and optimizations It's auto compiled, so you may expect better performance It's really fullstack: can develop a web app, compiler, web server,… It uses the asynchronous mechanism … Play Framework Play framework and laravel Laravel Play Framework • Using MVC architecture (Makes code much more manageable and easier to navigate) • Using MVC architecture (Makes code much more manageable and easier to navigate) • Auto-compiling (No wait time to see the changes that are made being implemented) • Auto-compiling (Java doesn’t normally this but Play has built it in) • • Fullstack: can develop a web app, compiler, web server Fullstack: can develop a web app, compiler, web server • • Evolutions Migration • • Java/Scala language PHP language • • Open-source (Doesn’t seem to be quite as extensive as Laravels but still big) Open Source (Loads of great addons and libraries to enhance the functionality of the system without having to custom code) Play Framework Environment Setup On local server(1) Create virtual host to run your app by adding following rows to file /etc/httpd/conf/httpd.conf ProxyPreserveHost On ServerName www.demo-scala.com ProxyPass /excluded ! ProxyPass / http://127.0.0.1:9500/ ProxyPassReverse / http://127.0.0.1:9500/ Install Java Software Development Kit (SDK) and set your java environment Sbt SBT is an open source build tool for Scala and Java projects, similar to Java’s Maven or Ant To install sbt please refer to http://www.scala-sbt.org/ Play Framework Environment Setup On local server(2) Typesafe-activator Activator is build upon sbt and can everything that sbt can do, plus more Activator New is a fast way to create new Scala projects from a range of different templates and start building them immediately It replaces the Play command in Play Framework Install activator Download activator: wget http://downloads.typesafe.com/typesafe-activator/1.3.2/typesafe-activator-1.3.2-minimal.zip And add the activator script to your PATH Ex: export PATH=$PATH:/usr/bin/activator-1.3.12/bin/ Create new project with exsiting template play-scala with command >activator new [project-name] play-scala >cd [project-name] Run project or run with port 9500 Play Framework >activator run >activator “run 9500” Deploy On Product Server Using the dist task builds a binary version of your app that you can deploy to a server having Java installation >sbt dist This produces a ZIP file containing all JAR files in target/universal folder of your app To run the app, unzip the file on target server, then run the script in the bin directory The name of the script is your app name >unzip demo-scala-1.0.zip >demo-scala-1.0/bin/demo-scala –Dplay.scrypto.secret=abcdefghijk You can also specify a different configuration file for production environment >demo-scala-1.0/bin/demo-scala –Dconfig.file=/full/path/to/conf/application-prod.conf Play Framework Structure play framework project Source code includes those important folders and files “app” folder: All scala main code of project is put at here “build.sbt” file: In this file you declare plugins library that using in project “conf” folder: In this folder has important files - “application.conf”: Set config of all plugins that you declare in build.sbt - “routes”: Define all application routes “public” folder: Include using css, js, image files in project Play Framework Mvc in play framework Play Framework View (1) Most of the application views are generated using an efficient templating system provided by play The Controller gets some interesting data from the model layer, and then applies a template to decorate these objects View are defined in the app/views folder Following a simple naming convention (e.g user.scala.html) Template parameters : A template is like a function, so it needs parameters, which must be declared at the top of the template file View can using template available Play Framework 10 Database Use the ScalikeJDBC library to connect database(MySQL) Add the MySQL JDBC dependency to the build.sbt file libraryDependencies ++= Seq( jdbc) libraryDependencies += "mysql" % "mysql-connector-java" % "5.1.24" Configuration mysql in conf/application.conf file db { default.driver = com.mysql.jdbc.Driver default.url = "jdbc:mysql://localhost/tien_scala“ default.username = root default.password = "vinasaver“ } Install ScalikeJDBC to run query, add to build.sbt libraryDependencies ++= Seq( "org.scalikejdbc" %% "scalikejdbc" % "2.4.1", "org.scalikejdbc" %% "scalikejdbc-config" % "2.4.1", "org.scalikejdbc" %% "scalikejdbc-play-initializer" % "2.5.1") Play Framework 17 evolutions Evolutions is used to create relational databases for easy tracking and organization CREATE TABLE users ( Add evolutions into your dependencies list (in build.sbt): libraryDependencies += evolutions # - !Ups Create evolutions script: The first script is named “1.sql”, the second script “2.sql”, and so on… and should be located in “conf/evolutions/default” If you agree with the SQL script, you can apply it directly by clicking on the ‘Apply evolutions’ button id int(10) NOT NULL AUTO_INCREMENT, name varchar(255) NOT NULL, email varchar(255) NOT NULL, password varchar(255) NOT NULL, api_token varchar(60) NOT NULL, created_at timestamp, updated_at timestamp, PRIMARY KEY (id) ); Play Framework 18 REST API(1) Method : GET Step 1: Add url routes GET /all controllers.UsersController.all() Step 2: Create function all() def all() = Action { implicit request => implicit val userFormat = Json.format[Users] val list_user = Users.getAllUsers() Ok(Json.obj("success"-> true, "data" -> list_user, "error" -> "" ) ) } Play Framework 17 REST API(2) Method : POST Step 1: Add url routes POST /test controllers.UsersController.test() Step 2: Create function test() def test() = Action.async(parse.urlFormEncoded) { implicit request => implicit val userFormat = Json.format[Users] val email = request.body("email").head val password = request.body("password").head val list_user = Users.authenication(email,password) Future(Ok(Json.obj("success"-> true, "error" -> "" ) "data" -> list_user, ) ) } Play Framework 18 Demo App(1) Step 1: Set application routes In conf/routes set routes like GET /login controllers.AuthenicationController.login POST /login controllers.AuthenicationController.auth GET /logout controllers.AuthenicationController.logout GET / controllers.UsersController.index GET /user controllers.UsersController.user GET /edit/:id controllers.UsersController.editpage(id: Int) POST /edit/:id controllers.UsersController.edit(id: Int) GET controllers.UsersController.addpage() /add POST /add GET /delete/:id controllers.UsersController.add() controllers.UsersController.delete(id: Int) After that run command line > activator run To see how does it look like For more functionalities in this app please refer to source code Play Framework 21 Demo App(2) Create screen login (login.scala.html) @(message:String) @page("Login page") { @message ID パパパパパパパパパパパ
- ※ パパパパパパパパパパパパパパパパパパパパパパパパパ パパパパパパパパパパパパパパパパパパパパパパパパパパパパ
- ※ パパパパパパパパパパパパパパパパパパパパパパ
- ※ パパパパパパパパパパパパパパパパパパパパ パパパパパパパパパパパパパパパパパパパパパパパパパパパパパパ