Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Play framework

7 views

Published on

An introduction to Play Framework enumerating most of the important feature of this awesome web framework

Published in: Software
  • Be the first to comment

  • Be the first to like this

Play framework

  1. 1. Play Framework
  2. 2. The big picture
  3. 3. The Big Picture (contd.) 1. Follows Async model of request response ○ Jetty ○ NIO 2. Not one thread per request 3. MVC design pattern separates application’s logic and data from the user interface’s 4. Brings in Scala type safety to good use 5. Great DX ○ Errors are shown on the browser ○ Auto reload, no messy build and redeployment
  4. 4. The Big Picture (contd.)
  5. 5. How do you create one? Use SBT sbt new playframework/play-scala-seed.g8 And you get this common play project structure
  6. 6. Different parts of Play Framework => =>
  7. 7. 1. The Routes file GET / controllers.Application.index POST /product controllers.Products.addProduct GET /products controllers.Products.list GET /product/:id controllers.Products.getProductById(id: Long) GET /product/byName controllers.Products.getProductByName(name: String) # Map static resources from the /public folder to the /assets URL path GET /assets/*file controllers.Assets.at(path="/public", file)
  8. 8. 2. The Controller ● Extend your class with play.mvc.Controller ● Write methods to handle calls in the routes file ● The Action class a. final def apply(block: => Result): Action[AnyContent] b. final def async(block: => Future[Result]): Action[AnyContent] c. final def async[A](bodyParser: BodyParser[A])(block: R[A] => Future[Result]): Action[A] ● controllers, actions, requests, and responses as Scala traits in the play.api.mvc package - controller layer API ● MVC API mixes the HTTP concepts (request, response), with MVC concepts such as controllers and actions.
  9. 9. 2. The Controller (contd.) => Important Classes/Traits Corresponding to HTTP concepts play.api.mvc.Cookie — An HTTP cookie: a small amount of data stored on the client and sent with subsequent requests play.api.mvc.Request — An HTTP request: HTTP method, URL, headers, body, and cookies play.api.mvc.RequestHeader — Request metadata: a name-value pair play.api.mvc.Response — An HTTP response, with headers and a body; wraps a Play Result play.api.mvc.ResponseHeader — Response metadata: a name-value pair
  10. 10. 2. The Controller (contd.) => Important Classes/Traits Corresponding to MVC and extras play.api.mvc.Action — A function that processes a client Request and returns a Result play.api.mvc.Result — The result of calling an Action to process a Request, used to generate an HTTP response play.api.mvc.Call — An HTTP request: the combination of an HTTP method and a URL play.api.mvc.Content — An HTTP response body with a particular content type play.api.mvc.Controller — A generator for Action functions play.api.mvc.Session — A set of string keys and values, stored in an HTTP cookie
  11. 11. 3. The View/Template 1. Type safe template engine 2. HTML 5 mark up with Scala 3. Easy to compose @import services.ProductsService.Product @(products: List[Product]) @main("Demo Product Application") { <dl class="products"> @for(product <- products) { <dt>@product.name</dt> <dd>@product.description</dd> } </dl> }
  12. 12. 4. Play JSON 1. Easy to write formats for models 2. Uses macro to generate JSON reads and writes ○ case class Product(id: Long, name: String, description: String) ○ implicit val productFormat = Json.format[Product] 3. Built on top of Jackson
  13. 13. 5. Public Assets GET /assets/*file controllers.Assets.at(path="/public", file) 1. Treated as any other route so easier to use the reverse route without using the actual path in all the templates 2. *file part matches anything including the / GET /assets/*file controllers.Assets.versioned(path="/public", file: Asset) 1. automatically manages ETag HTTP Headers. 2. When a web browser makes a request specifying this Etag then the server can respond with 304 NotModified.
  14. 14. 6. Internationalization with Messages conf/messages—Default messages for all languages, for messages not localized for a particular language conf/messages.es—Spanish (which is called Español in Spanish) conf/messages.fr—French (Français in French) conf/messages.nl—Dutch (Nederlands in Dutch) => Inject MessagesApi and call the apply method with the key and varargs as message arguments and you are done!
  15. 15. Request/Response Flow play.api.mvc.Result — The result of calling an Action to process a Request, used to generate an HTTP response
  16. 16. Resources 1. Play’s official docs - https://www.playframework.com/documentation 2. Play For Scala (Book) - https://www.manning.com/books/play-for-scala 3. Talks - a. The Play Framework at LinkedIn: Productivity and Performance at Scale - https://www.youtube.com/watch?v=8z3h4Uv9YbE b. James Ward: Modern Play Apps with Play Framework - https://www.youtube.com/watch?v=Dfek3hIVRtw
  17. 17. Thank You!

×