Your First Scala Web Application
using Play! Framework
By Matthew Barlocker
● Chief Architect at Lucid
● Learned Scala and Play!
“the hard way”.
● Graduated with Bachelors
Degree in Computer Science
from BYU in 2008.
● Developed software for the
– Network Security
– Social Gaming
Time is Scarce!
Write your questions down
and ask me after the session.
Get it Running
● Requires Java >= 6 to run
● You need a terminal.
● Add play-2.1.4 directory to PATH
● `export PATH=$PATH:/path/play-2.1.4`
● `chmod +x /path/play-2.1.4/play`
● Add path to global environment variables
● Don't use a path with spaces
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
● 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.
Scala Intro - Variables
– good for multi-threading
– recommended type of variable
– good for procedural code
Scala Intro - Typing
● Colon-postfix notation for typing.
● All variables and functions can be explicitly
● Some variables and functions can be implicitly
Scala Intro - Looping
● Most looping is done over lists, arrays, maps,
and other data structures using .foreach() or
● for (i <- 0 until 10)
– Looping criteria (in parens) is calculated exactly
– Can be used to 'yield' results.
● [do …] while (i < 10)
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.
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.
Scala Intro - Collections
● Tuples have a length and each element has a
– 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)
Scala Intro - Classes
● Case classes get the following for free:
– 'equals', 'toString', 'hashCode', 'copy' functions.
– every class argument is a public val unless
● Objects are singleton classes.
– Must be used for static methods.
● Traits are abstract classes.
– No class arguments.
– Used for multiple inheritance or interfaces.
● 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-
● Make your changes and simply hit refresh! All
you need is a browser and a text editor.
● Rebuilds the project when you change files and
refresh the page.
● IDE support for IntelliJ, Eclipse, Sublime, and
● Asset compiler for LESS, CoffeeScript, and
● JSON is a first-class citizen.
Request Handling - Terminology
● Route – Mapping of URL/HTTP Method to an
● 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
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:
● Create a TODO page
● Use URL parameters and action to send 404
● 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.
● '@' is the magical operator.
● No special functions, it's just embedded Scala code.
● Each view is just a function that can be called from
● Views set the content type automatically.
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.
● 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.
Forms - Exercise
● Create a contact form.
● Create a login form. On submit, set a fake user
id in the session.
● Form.bindFromRequest will use GET and
● There are many constraints and data types.
Explore to find them.
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.
● 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'
I18n - Exercise
● Replace all messages in the layout.
● Add language files
● Configure another language
● Try it in the browser
Testing - Exercise
● Inspect and modify existing tests.
● Run tests from command line.
● Advanced Request Handling
● Application Global
Lucid Software Inc
● Building the next generation of collaborative web
● VC funded, high growth, profitable
● Graduates from Harvard, MIT, Stanford
● Team has worked at Google, Amazon, Microsoft