O slideshow foi denunciado.
Seu SlideShare está sendo baixado. ×

Your First Scala Web Application using Play 2.1

Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Carregando em…3
×

Confira estes a seguir

1 de 41 Anúncio
Anúncio

Mais Conteúdo rRelacionado

Diapositivos para si (20)

Quem viu também gostou (20)

Anúncio

Semelhante a Your First Scala Web Application using Play 2.1 (20)

Anúncio

Mais recentes (20)

Your First Scala Web Application using Play 2.1

  1. 1. Your First Scala Web Application using Play! Framework By Matthew Barlocker
  2. 2. The Barlocker ● Chief Architect at Lucid Software Inc ● Learned Scala and Play! “the hard way”. ● Graduated with Bachelors Degree in Computer Science from BYU in 2008. ● Developed software for the following industries: – Network Security – Social Gaming – Financial – Productivity
  3. 3. Time is Scarce! Write your questions down and ask me after the session.
  4. 4. Quick Assessment ● Languages – Scala – Java – PHP – Ruby ● MVC Frameworks ● Build Tools – sbt – maven – ivy
  5. 5. Get it Running ● Requires Java >= 6 to run ● You need a terminal. ● Add play-2.1.4 directory to PATH – Linux ● `export PATH=$PATH:/path/play-2.1.4` ● `chmod +x /path/play-2.1.4/play` – Windows ● Add path to global environment variables ● Don't use a path with spaces
  6. 6. Get it Running ● `cd /parent/dir/for/new/project` ● `play new strangeloop` ● `cd strangeloop` ● `git init` - suggested ● `play [debug]` – `run [port=9000]` ● Go to http://localhost:9000
  7. 7. Scala Intro ● The current version is 2.10.2. ● Open your cheat sheet for code samples. ● Runs on the JVM. ● 100% inter-operable with Java code (jars). ● Functions are first-class citizens. ● Typed, compiled language. ● Semicolons are inferred. ● Functional language that supports procedural.
  8. 8. Scala Intro - Variables ● val – immutable – good for multi-threading – recommended type of variable ● var – mutable – good for procedural code
  9. 9. Scala Intro - Typing ● Colon-postfix notation for typing. ● All variables and functions can be explicitly typed. ● Some variables and functions can be implicitly typed.
  10. 10. Scala Intro - Looping ● Most looping is done over lists, arrays, maps, and other data structures using .foreach() or .map(). ● for (i <- 0 until 10) – Looping criteria (in parens) is calculated exactly once. – Can be used to 'yield' results. ● [do …] while (i < 10)
  11. 11. Scala Intro – Control Structures ● if (condition) … else … – Returns a value. – Use instead of a ternary operator. ● match statement – Similar to a switch, but much more flexible. – Can match regular expressions, interfaces, classes, and other extractors.
  12. 12. Scala Intro - Functions ● 'public' is the default access modifier. ● The last value computed is returned. ● Function names can include operators. – '+' is a function on strings and numbers. ● Parameters must be typed. ● Return value can be inferred. ● Multiple parameter lists are allowed. Not the same as currying.
  13. 13. Scala Intro - Collections ● Tuples have a length and each element has a type. – val a = (5, 2.0, “hello”) ● Maps are key -> value pairs – val b = Map(1 -> “a”, 2 -> “b”) ● Arrays are mutable – val c = Array(4, 5, 6) ● Lists are immutable – val d = List(7, 8, 9)
  14. 14. Scala Intro - Classes ● Case classes get the following for free: – 'equals', 'toString', 'hashCode', 'copy' functions. – every class argument is a public val unless specified otherwise. ● Objects are singleton classes. – Must be used for static methods. ● Traits are abstract classes. – No class arguments. – Used for multiple inheritance or interfaces.
  15. 15. Scala Intro – Console Example ● Variables ● Functions ● Classes ● Options ● Matching ● Lists, Maps ● Iterating ● Function parameters ● Parameter Lists ● Conditionals
  16. 16. Scala Resources ● http://www.scala-lang.org/ ● http://www.scala-lang.org/documentation/ ● https://groups.google.com/d/forum/scala-user ● irc://irc.freenode.net/scala ● http://www.scala-lang.org/community/ ● https://github.com/scala/scala
  17. 17. Play! Intro ● Current version is 2.1.4. ● Play Framework makes it easy to build web applications with Java & Scala. ● Play is based on a lightweight, stateless, web- friendly architecture. ● Make your changes and simply hit refresh! All you need is a browser and a text editor.
  18. 18. Play! Features ● Rebuilds the project when you change files and refresh the page. ● IDE support for IntelliJ, Eclipse, Sublime, and more. ● Asset compiler for LESS, CoffeeScript, and more. ● JSON is a first-class citizen.
  19. 19. Who Uses Play!
  20. 20. Play! Resources ● http://www.playframework.com/ ● https://github.com/playframework/playframework ● https://groups.google.com/group/play-framework ● http://www.playframework.com/documentation ● http://twitter.com/playframework
  21. 21. Let's Build It!
  22. 22. Topics ● Request Handling – URLs – Controllers – Actions – Responses – HTTP ● Views – Templates – Encoding – Assets ● Forms – Validation – Submission
  23. 23. Topics (cont.) ● Database – Evolutions – Connections – Models – Queries ● Build System – Dependencies – Deployment – Testing ● Application Global – Request Handling – Error Handling – Application Hooks ● I18n – Strings – Views – Configuration
  24. 24. Topics (cont.) ● Testing – Fake Application – Fake Requests – Fake DB – Patterns
  25. 25. Reminder: Write your questions down
  26. 26. Request Handling - Terminology ● Route – Mapping of URL/HTTP Method to an action ● Action – Function that takes a request and returns a result ● Controller – Action generator ● Request – HTTP headers and body ● Result – HTTP status code, headers, and body
  27. 27. Request Handling - Exercise ● Create a new home page ● Create a page that redirects to the new home page ● Set content type on home page ● Create a page to set, and a page to get: – Headers – Cookies – Session – Flash ● Create a TODO page ● Use URL parameters and action to send 404
  28. 28. SimpleResults ● Ok ● Created ● Accepted ● MovedPermanently ● Found ● SeeOther ● NotModified ● TemporaryRedirect ● BadRequest ● Unauthorized ● Forbidden ● NotFound ● InternalServerError ● ...
  29. 29. Routes ● Every route has HTTP method, URL, and action to call. ● URL can include parameters, which are passed to the action. ● These parameters can be validated and converted as part of the matching. ● First matching route wins.
  30. 30. Views ● '@' is the magical operator. ● No special functions, it's just embedded Scala code. ● Each view is just a function that can be called from anywhere. ● Views set the content type automatically.
  31. 31. Views - Exercise ● Create view for URL parameter page. ● Create view for the home page. ● Create template, use it in the home page view. ● Add links to template ● Display flash messages in layout. ● Use implicit request.
  32. 32. Views ● Files are named package/myview.scala.html. ● Views are referenced views.html.package.myview. ● All values are HTML encoded for you. ● Views are not intended to handle big data. ● If broken, play with the whitespace.
  33. 33. Forms - Exercise ● Create a contact form. ● Create a login form. On submit, set a fake user id in the session.
  34. 34. Forms ● Form.bindFromRequest will use GET and POST variables. ● There are many constraints and data types. Explore to find them.
  35. 35. Database - Exercise ● Configure a database connection. ● Create a database evolution for users table. ● Create page and model to register users. ● Update login to check against user list. ● Create page to show current user.
  36. 36. Build System ● Reload play after changing dependencies. ● Find dependencies at http://mvnrepository.com/ ● Try a deployment – `play stage` – `./target/start -Dhttp.port=9000` ● To deploy, copy target/start and target/staged to the target system, and run 'start'
  37. 37. I18n - Exercise ● Replace all messages in the layout. ● Add language files ● Configure another language ● Try it in the browser
  38. 38. Testing - Exercise ● Inspect and modify existing tests. ● Run tests from command line.
  39. 39. Time Permitting ● Advanced Request Handling – EssentialAction – RequestHeader – Iteratees – Filters ● Application Global
  40. 40. Thank you for your time. Any Questions?
  41. 41. Lucid Software Inc ● Building the next generation of collaborative web applications ● VC funded, high growth, profitable ● Graduates from Harvard, MIT, Stanford ● Team has worked at Google, Amazon, Microsoft https://www.lucidchart.com/jobs

×