Scalatra

From air
Jump to navigation Jump to search

Scalatra is an HTTP micro-framework, written in the up-and-coming new language, Scala. Scalatra is used by LinkedIn, the BBC, the Guardian newspaper, games website IGN, social identity provider JanRain, the Wordnik online dictionary, and the British government’s new flagship website. It’s fast, battle-tested, and in production. This article introduces you to the simplicity of Scalatra micro-framework.

Install

http://www.scalatra.org/2.2/getting-started/installation.html

javac -version
java -version
curl https://raw.githubusercontent.com/n8han/conscript/master/setup.sh > install.sh
sh install.sh

PATH=$PATH:~/bin
export PATH
source ~/.bash_profile # (Mac)
source ~/.bashrc       # (Linux)

cs n8han/giter8

Example of web app

(from Book Scalatra in Action)

Generate the project

g8 scalatra/scalatra-sbt
package com.example.app
import org.scalatra._
import scalate.ScalateSupport
class MyScalatraServlet extends ScalatraServlet with ScalateSupport {
  get(“/”) {
    <html>
    <body>
    <h1>Hello, world!</h1>
    Say <a href=“hello-scalate”>hello to Scalate</a>.
    </body>
    </html>
  }
  notFound {
    // remove content type in case it was set through an action
    contentType = null
    // Try to render a ScalateTemplate if no route matched
    findTemplate(requestPath) map { path =>
      contentType = “text/html”
      layoutTemplate(path)
   } orElse serveStaticResource() getOrElse resourceNotFound()
  }
}


Start the web container

container:start

browse http://localhost:8080/hello

Books