1. Play ! Framework
Introduction & Highlights
Malti Yadav
malti@knoldus.com
2. Quick Overview
Stateless framework
Integration of JSON
Full stack web framework
Compilation and Exact Error
Lots of build in feature for fast developement
Urls are entry poin to application
RESTful by default
5. Standard Application Layout
app → Application sources
└ assets → Compiled asset sources
└ stylesheets → Typically LESS CSS sources
└ javascripts → Typically CoffeeScript sources
└ controllers → Application controllers
└ models → Application business layer
└ views → Templates
build.sbt → Application build script
conf → Configurations files and other non-compiled resources (on classpath)
└ application.conf → Main configuration file
└ routes → Routes definition
public → Public assets
└ stylesheets → CSS files
└ javascripts → Javascript files
└ images → Image files
project → sbt configuration files
└ build.properties → Marker for sbt project
└ plugins.sbt → sbt plugins including the declaration for Play itself
lib → Unmanaged libraries dependencies
logs → Standard logs folder
└ application.log → Default log file
target → Generated stuff
└ scala-2.10.0
└ cache
└ classes → Compiled class files
└ classes_managed → Managed class files (templates, ...)
└ resource_managed → Managed resources (less, ...)
└ src_managed → Generated sources (templates, ...)
test → source folder for unit or functional tests
6. The conf/routes file
# Routes
# This file defines all application routes (Higher priority routes first)
# ~~~~
# Home page
GET / controllers.Application.index
# Registration page
GET /registration controllers.Application.registration
# Login page
GET /login controllers.Application.login
# logout
GET /logout controllers.Application.logout
#save userdata
POST /registration controllers.Application.createUser
# loginAuthentication
POST /login controllers.Application.loginAuthentication
# Dashboard page
GET /dashboard controllers.Application.dashboard
# Map static resources from the /public folder to the /assets URL path
GET /assets/*file controllers.Assets.at(path="/public", file)
7. The conf/application file
Configure database access
mongodb.servers=[“localhost:27017”]
mongodb.db=”test”
Logger
Router
Specific modules
8. Designing a domain models
package models
import java.util.Date
object Domains {
/*
* Case class for UserForm
*/
case class UserForm(name:String,email:String,password:
(String,String),joinDate: Date)
/*
* Case class for LoginForm
*/
case class LoginForm(email:String,password: String)
}
9. Designing a QueryBuilder models
package models
import play.api._
import play.api.mvc._
import play.api.libs.functional.syntax._
import play.api.libs.json._
import play.api.libs.concurrent.Execution.Implicits.defaultContext
import scala.concurrent.Future
import reactivemongo.api._
import play.modules.reactivemongo.MongoController
import play.modules.reactivemongo.json.collection.JSONCollection
object QueryBuilder extends Controller with MongoController {
/*
* Get a JSONCollection (a Collection implementation that is designed to work
* with JsObject, Reads and Writes.)
* Note that the `collection` is not a `val`, but a `def`. We do _not_ store
* the collection reference to avoid potential problems in development with
* Play hot-reloading.
*/
def collection(collectionname: String): JSONCollection =
db.collection[JSONCollection](collectionname)
11. Designing a helper class
package utils
import play.api.mvc.RequestHeader
object Helper {
/*
* find URI
* @param request
*/
def findUri(request: RequestHeader): String = {
request.uri
}
12. Cont...
/*
* find session values
* @param request
* @param element - find session value stored in element
*/
def findSession(request: RequestHeader, element: String): String = {
request.session.get(element).map { sessionvalue =>
sessionvalue
}.getOrElse {
" "
}
}
13. Cont...
/*
* find flash values
* @param request - HTTP request
* @param element - find flash value stored in element
*/
def findFlash(request: RequestHeader, element: String): String = {
request.flash.get(element).getOrElse {
" "
}
}
}
14. Designing a constants class
package utils
object Constants {
val USER_COLLECTION_NAME = "user"
val MIN_LENGTH_OF_PASSWORD = 6
val PASSWORD_NOT_MATCHED = "Password must be matched"
val HOME_PAGE_TITLE = "Welcome to home page"
val WRONG_LOGIN_DETAILS = "The email or password you entered is incorrect"
val DASHBOARD_TITLE = "Welcome to dashboard"
val ENTERED_EMAIL_EXISTS = "Entered email id already exist in database"
val DEFAULT_PROFILE_IMAGE = "profile.jpeg"
}