Driving Behavioral Change for Information Management through Data-Driven Gree...
Your First Scala Web Application using Play 2.1
1. Your First Scala Web Application
using Play! Framework
By Matthew Barlocker
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
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. 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. 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. Scala Intro - Variables
● val
– immutable
– good for multi-threading
– recommended type of variable
● var
– mutable
– good for procedural code
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. 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. 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. 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. 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. 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.
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. 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.
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. 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. SimpleResults
● Ok
● Created
● Accepted
● MovedPermanently
● Found
● SeeOther
● NotModified
● TemporaryRedirect
● BadRequest
● Unauthorized
● Forbidden
● NotFound
● InternalServerError
● ...
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. 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. 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. 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. Forms - Exercise
● Create a contact form.
● Create a login form. On submit, set a fake user
id in the session.
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. 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. I18n - Exercise
● Replace all messages in the layout.
● Add language files
● Configure another language
● Try it in the browser
38. Testing - Exercise
● Inspect and modify existing tests.
● Run tests from command line.
39. Time Permitting
● Advanced Request Handling
– EssentialAction
– RequestHeader
– Iteratees
– Filters
● Application Global
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