On Github jedichenbin / IntroPlay
void doGet(HttpServletRequest req, HttpServletResponse res) {
// Apache HttpClient
HttpClient client = new HttpClient();
GetMethod method = new GetMethod("www.foobar.com");
// blocking until return from synchronous call
int status = client.executeMethod(method);
LOG.log("Response status code is {}", status);
}
public Result index(String url) {
F.Promise responsePromise = WS.url(url).get();
// thanks to the non-blocking IO, thread won't be idle here
return async(responsePromise.map(
new F.Function<ws.response, Result>() {
@Override
public Result apply(WS.Response response)
throws Throwable {
return ok(response.getBody()).as("text/html");
}
}
));
} .response>
// make two parallel async calls
val fooFuture = WS.url(url1).get()
val barFuture = WS.url(url2).get()
for {
foo <- fooFuture
bar <- barFuture
} yield {
Ok(/*...*/)
}
app → Application sources └ assets → Compiled asset sources └ stylesheets → Typically LESS CSS sources └ javascripts → Typically CoffeeScript sources └ controllers → Application controllers └ models → Application business layer └ views → Templates conf → Configurations files └ application.conf → Main configuration file └ routes → Routes definition public → Public assets └ stylesheets → CSS files └ javascripts → Javascript files └ images → Image files project → sbt configuration files └ build.properties → Marker for sbt project └ Build.scala → Application build script └ plugins.sbt → sbt plugins lib → Unmanaged libraries dependencies test → source folder for unit or functional tests
Action { request =>
Ok("Got request [" + request + "]")
}
(play.api.mvc.Request => play.api.mvc.Result)
package controllers
import play.api.mvc._
object Application extends Controller {
def index = Action {
Ok("It works!")
}
}
# Extract the page parameter from the path GET /:page controllers.Application.show(page)
GET /clients/all controllers.Clients.list()
GET /clients/:id controllers.Clients.show(id: Long)
GET /clients/$id<[0-9]+> controllers.Clients.show(id: Long)
# Fix the path to '/' if the requested path is home GET / controllers.Application.show(page = "home")
# Pagination links, like /clients?page=3 GET /clients controllers.Clients.list(page: Int ?= 1)
# The version parameter is optional. E.g. /api/list-all?version=3.0 GET /api/list-all controllers.Api.list(Option[version])
// Redirect to /hello/Bob
def helloBob = Action {
Redirect(routes.Application.hello("Bob"))
}
For each controller used in the routes file,
the router will generate a ‘reverse controller’,
having the same action methods,
with the same signature,
but returning a play.api.mvc.Call.
@(customer: Customer, orders: List[Order])
<h1>Welcome @customer.name!</h1>
<ul>
@for(order <- orders) {
<li>@order.getTitle()</li>
}
</ul>