Your First Scala Web Application using Play 2.1
-
Upload
matthew-barlocker -
Category
Technology
-
view
5.114 -
download
1
description
Transcript of Your First Scala Web Application using Play 2.1
Your First Scala Web Applicationusing Play! Framework
By Matthew Barlocker
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
Time is Scarce!
Write your questions downand ask me after the session.
Quick Assessment
● Languages– Scala
– Java
– PHP
– Ruby
● MVC Frameworks● Build Tools
– sbt
– maven
– ivy
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
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
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.
Scala Intro - Variables
● val– immutable
– good for multi-threading
– recommended type of variable
● var– mutable
– good for procedural code
Scala Intro - Typing
● Colon-postfix notation for typing.● All variables and functions can be explicitly
typed.● Some variables and functions can be implicitly
typed.
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)
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 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)
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.
Scala Intro – Console Example
● Variables● Functions● Classes● Options● Matching
● Lists, Maps● Iterating● Function parameters● Parameter Lists● Conditionals
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
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.
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.
Who Uses Play!
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
Let's Build It!
Topics
● Request Handling– URLs
– Controllers
– Actions
– Responses
– HTTP
● Views– Templates
– Encoding
– Assets
● Forms– Validation
– Submission
Topics (cont.)
● Database– Evolutions
– Connections
– Models
– Queries
● Build System– Dependencies
– Deployment
– Testing
● Application Global– Request Handling
– Error Handling
– Application Hooks
● I18n– Strings
– Views
– Configuration
Topics (cont.)
● Testing– Fake Application
– Fake Requests
– Fake DB
– Patterns
Reminder:
Write your questions down
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
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
SimpleResults
● Ok● Created● Accepted● MovedPermanently● Found● SeeOther● NotModified
● TemporaryRedirect● BadRequest● Unauthorized● Forbidden● NotFound● InternalServerError● ...
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.
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.
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.
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.
Forms - Exercise
● Create a contact form.● Create a login form. On submit, set a fake user
id in the session.
Forms
● Form.bindFromRequest will use GET and POST variables.
● 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.
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'
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.
Time Permitting
● Advanced Request Handling– EssentialAction
– RequestHeader
– Iteratees
– Filters
● Application Global
Thank you for your time.Any Questions?
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