Scala Play from scratch
If DropWizard excels at RESTful webservices, then the Play Framework is often the counterpart which provides an HTML front-end. Although it’s got both Java and Scala support, the Scala support seems to be more active. Here’s an introduction without using all the Activator Framework / play create app template commands.
First, create a directory to work in
$ mkdir LearnPlay $ cd LearnPlay |
Next, you’ll need SBT (the build tool). Here we’re using the latest version (0.13.7). Follow the instructions to save it to the root of your working folder, alongside an SBT shell script to launch it:-
$ wget https://repo.typesafe.com/typesafe/ivy-releases/org.scala-sbt/sbt-launch/0.13.7/sbt-launch.jar $ cat << EOF > sbt SBT_OPTS="-Xms512M -Xmx1536M -Xss1M -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=256M" java \$SBT_OPTS -jar \`dirname \$0\`/sbt-launch.jar "\$@" EOF $ chmod 0755 sbt |
Create a project/build.properties file which supplies the version of SBT:-
$ mkdir -p project $ cat << EOF > project/build.properties sbt.version=0.13.7 EOF |
Add an SBT plugin file to include the Play Framework plugins:-
$ cat << EOF > project/plugins.sbt // The Typesafe repository resolvers += "Typesafe repository" at "https://repo.typesafe.com/typesafe/releases/" // Use the Play sbt plugin for Play projects addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.3.8") EOF |
// Use the Play sbt plugin for Play projects
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.3.8")
EOF
Define a buildfile for your project (note that the spaces between lines are mandatory)
$ cat << EOF > build.sbt name := "LearnPlay" version := "1.0.0-SNAPSHOT" lazy val root = (project in file(".")).enablePlugins(PlayScala) EOF |
version := "1.0.0-SNAPSHOT"
lazy val root = (project in file(".")).enablePlugins(PlayScala)
EOF
That’s all the basic Play build time infrastructure set up. Now we move on to creating your application-specific parts.
Create a conf/application.conf file to hold your default crypto key (for session signing and default Crypto calls):-
$ mkdir -p conf $ cat << EOF > conf/application.conf application.secret="this_will_be_replaced_shortly" EOF |
Make Play generate a new application secret
$ ./sbt play-update-secret |
Next we need a conf/routes file which maps the URLs to the application controllers
$ cat << EOF > conf/routes GET / controllers.Application.index EOF |
This says “for any GET / request, use the controllers.Application.index function”. So we need that too.
$ mkdir -p app/controllers $ cat << EOF > app/controllers/Application.scala package controllers import play.api.mvc._ object Application extends Controller { def index = TODO } EOF |
import play.api.mvc._
object Application extends Controller {
def index = TODO
}
EOF
Now you can fire up your first basic Scala Play app
$ ./sbt ~run |
and point your web browser to http://localhost:9000 to see the result – a purple TODO page. (Note that I used a tilde before the run command above – this puts it in reloadable mode, so that any changes to the source files are recompiled without having to restart SBT).
Not hugely exciting, so let’s change it.
Modify the app/controllers/Application.scala and replace
def index = TODO |
with
def index = Action { Ok("Success") } |
and refresh your browser to see a simple Success message.
Hardly HTML, right? So now we can explore the Templates function of Play.
First, we need a HTML template
$ mkdir -p app/views $ cat << EOF > app/views/index.scala.html @(message: String) <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> </head> <body> <p>Hello @message</p> </body> </html> EOF |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
</head>
<body>
<p>Hello @message</p>
</body>
</html>
EOF
This creates a Play View which takes a single string parameter called “message”. We use this later to say hello in the body. Now modify the app/controllers/Application.scala file to use this view.
def index = Action { Ok(views.html.index("world!")) } |
See that we’re passing “world!” to the template. Reload your browser to see Hello World in all it’s glory.
Code available via Git under tag 1.0, and deployed to Heroku.
Well presented starting point. I am following these and hope you will have time to keep adding new posts.
You’re very welcome. Hope everything is good with you!