your first scala web application using play 2.1

41
Your First Scala Web Application using Play! Framework By Matthew Barlocker

Upload: matthew-barlocker

Post on 11-May-2015

5.113 views

Category:

Technology


1 download

DESCRIPTION

Originally presented as a workshop at Strangeloop 2013 in St Louis, MO.

TRANSCRIPT

Page 1: Your First Scala Web Application using Play 2.1

Your First Scala Web Applicationusing Play! Framework

By Matthew Barlocker

Page 2: Your First Scala Web Application using Play 2.1

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

Page 3: Your First Scala Web Application using Play 2.1

Time is Scarce!

Write your questions downand ask me after the session.

Page 4: Your First Scala Web Application using Play 2.1

Quick Assessment

● Languages– Scala

– Java

– PHP

– Ruby

● MVC Frameworks● Build Tools

– sbt

– maven

– ivy

Page 5: Your First Scala Web Application using Play 2.1

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

Page 6: Your First Scala Web Application using Play 2.1

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

Page 7: Your First Scala Web Application using Play 2.1

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.

Page 8: Your First Scala Web Application using Play 2.1

Scala Intro - Variables

● val– immutable

– good for multi-threading

– recommended type of variable

● var– mutable

– good for procedural code

Page 9: Your First Scala Web Application using Play 2.1

Scala Intro - Typing

● Colon-postfix notation for typing.● All variables and functions can be explicitly

typed.● Some variables and functions can be implicitly

typed.

Page 10: Your First Scala Web Application using Play 2.1

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)

Page 11: Your First Scala Web Application using Play 2.1

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.

Page 12: Your First Scala Web Application using Play 2.1

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.

Page 13: Your First Scala Web Application using Play 2.1

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)

Page 14: Your First Scala Web Application using Play 2.1

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.

Page 15: Your First Scala Web Application using Play 2.1

Scala Intro – Console Example

● Variables● Functions● Classes● Options● Matching

● Lists, Maps● Iterating● Function parameters● Parameter Lists● Conditionals

Page 16: Your First Scala Web Application using Play 2.1

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

Page 17: Your First Scala Web Application using Play 2.1

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.

Page 18: Your First Scala Web Application using Play 2.1

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.

Page 19: Your First Scala Web Application using Play 2.1

Who Uses Play!

Page 20: Your First Scala Web Application using Play 2.1

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

Page 21: Your First Scala Web Application using Play 2.1

Let's Build It!

Page 22: Your First Scala Web Application using Play 2.1

Topics

● Request Handling– URLs

– Controllers

– Actions

– Responses

– HTTP

● Views– Templates

– Encoding

– Assets

● Forms– Validation

– Submission

Page 23: Your First Scala Web Application using Play 2.1

Topics (cont.)

● Database– Evolutions

– Connections

– Models

– Queries

● Build System– Dependencies

– Deployment

– Testing

● Application Global– Request Handling

– Error Handling

– Application Hooks

● I18n– Strings

– Views

– Configuration

Page 24: Your First Scala Web Application using Play 2.1

Topics (cont.)

● Testing– Fake Application

– Fake Requests

– Fake DB

– Patterns

Page 25: Your First Scala Web Application using Play 2.1

Reminder:

Write your questions down

Page 26: Your First Scala Web Application using Play 2.1

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

Page 27: Your First Scala Web Application using Play 2.1

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

Page 28: Your First Scala Web Application using Play 2.1

SimpleResults

● Ok● Created● Accepted● MovedPermanently● Found● SeeOther● NotModified

● TemporaryRedirect● BadRequest● Unauthorized● Forbidden● NotFound● InternalServerError● ...

Page 29: Your First Scala Web Application using Play 2.1

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.

Page 30: Your First Scala Web Application using Play 2.1

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.

Page 31: Your First Scala Web Application using Play 2.1

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.

Page 32: Your First Scala Web Application using Play 2.1

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.

Page 33: Your First Scala Web Application using Play 2.1

Forms - Exercise

● Create a contact form.● Create a login form. On submit, set a fake user

id in the session.

Page 34: Your First Scala Web Application using Play 2.1

Forms

● Form.bindFromRequest will use GET and POST variables.

● There are many constraints and data types. Explore to find them.

Page 35: Your First Scala Web Application using Play 2.1

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.

Page 36: Your First Scala Web Application using Play 2.1

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'

Page 37: Your First Scala Web Application using Play 2.1

I18n - Exercise

● Replace all messages in the layout.● Add language files● Configure another language● Try it in the browser

Page 38: Your First Scala Web Application using Play 2.1

Testing - Exercise

● Inspect and modify existing tests.● Run tests from command line.

Page 39: Your First Scala Web Application using Play 2.1

Time Permitting

● Advanced Request Handling– EssentialAction

– RequestHeader

– Iteratees

– Filters

● Application Global

Page 40: Your First Scala Web Application using Play 2.1

Thank you for your time.Any Questions?

Page 41: Your First Scala Web Application using Play 2.1

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