On Github jamesward / introduction_to_the_play_framework-java
typesafe.com/platform/getstarted
Create a New Play App
activator new
IDE Support
activator idea
Run the App
activator ~run
Open the App
http://localhost:9000VERB PATH CONTROLLER_METHOD GET / controllers.Application.index() GET /foo controllers.Application.foo()
public static Result index() { return ok(index.render("hello, world")); }
@(message: String) @main("Welcome to Play 2.0") { @message }
Run All Tests Once
activator test
Run All Tests Continuously
activator ~test
Run One Test
activator "test-only my.namespace.MySpec"
Run Failed Tests
activator test-quick
@Test public void indexTemplate() { Content html = views.html.index.render("test"); assertThat(contentType(html)).isEqualTo("text/html"); assertThat(contentAsString(html)).contains("test"); }
@Test public void callIndex() { running(fakeApplication(), new Runnable() { public void run() { Result result = callAction(controllers.routes.ref.Application.index()); assertThat(status(result)).isEqualTo(OK); assertThat(contentType(result)).isEqualTo("text/html"); assertThat(charset(result)).isEqualTo("utf-8"); assertThat(contentAsString(result)).contains("Your new application is ready."); } }); }
@Test public void fooRoute() { running(fakeApplication(), new Runnable() { public void run() { Result result = route(fakeRequest("GET", "/foo")); assertThat(result).isNotNull(); } }); }
db.default.driver=org.h2.Driver db.default.url="jdbc:h2:mem:play" ebean.default="models.*"
package models; import play.db.ebean.Model; import javax.persistence.Entity; import javax.persistence.Id; @Entity public class Bar extends Model { @Id public String id; public String name; }
@Test public void create() { running(fakeApplication(), new Runnable() { public void run() { Bar bar = new Bar(); bar.name = "Write a test"; bar.save(); assertThat(bar.id).isNotNull(); } }); }
public static Result addBar() { Bar bar = Form.form(Bar.class).bindFromRequest().get(); bar.save(); return redirect(routes.Application.index()); }
POST /bars controllers.Application.addBar()
@helper.form(action = routes.Application.addBar()) { <input name="name"/> <input type="submit"/> }
public static Result getBars() { List<Bar> bars = new Model.Finder(String.class, Bar.class).all(); return ok(toJson(bars)); }
GET /bars controllers.Application.getBars()
@Constraints.Required public String name;
public static Result addBar() { Form<Bar> form = form(Bar.class).bindFromRequest(); if (form.hasErrors()) { return badRequest(index.render(form)); } else { // save } }
<script src='@routes.Assets.at("javascripts/foo.min.js")'></script>
$ -> $.get "/bars", (data) -> $.each data, (index, bar) -> $("#bars").append $("<li>").text bar.name
<script src='@routes.Assets.at("javascripts/index.min.js")'>
"org.webjars" %% "webjars-play" % "2.2.0", "org.webjars" % "bootstrap" % "2.3.1"Create Route for WebJarAssets Controller
GET /webjars/*file controllers.WebJarAssets.at(file)Use a WebJar
<script src='@routes.WebJarAssets.at(WebJarAssets.locate("jquery.min.js"))'></script>
public static Result index() { return ok(views.html.index.render("hello")); }
public static F.Promise<Result> index() { return F.Promise.promise(new F.Function0<Result>() { public Result apply() { return ok(views.html.index.render("hello")); } }); }
public static F.Promise<Result> index() { return F.Promise.delayed(new F.Function0<Result>() { public Result apply() throws Throwable { return ok(views.html.index.render("hello")); } }, 5, TimeUnit.SECONDS); }
public static F.Promise<Result> index() { F.Promise<WS.Response> jw = WS.url("http://www.jamesward.com").get(); return jw.map(new F.Function<WS.Response, Result>() { public Result apply(WS.Response response) throws Throwable { return ok(response.getBody()); } }); }
public static F.Promise<Result> index() { final F.Promise<WS.Response> jw = WS.url("http://www.jamesward.com").get(); final F.Promise<WS.Response> tw = WS.url("http://www.twitter.com").get(); return jw.flatMap(new F.Function<WS.Response, F.Promise<Result>>() { public F.Promise<Result> apply(final WS.Response jwR) throws Throwable { return tw.map(new F.Function<WS.Response, Result>() { public Result apply(WS.Response twR) throws Throwable { return ok(twR.getBody() + jwR.getBody()); } }); } }); }
public static Result events() { EventSource eventSource = new EventSource() { public void onConnected() { sendData("hello"); } }; return ok(eventSource); } $ -> events = new EventSource("/events") events.onmessage = (e) -> console.log(e.data)
public static WebSocket<String> echo() { return new WebSocket<String>() { public void onReady(final In<String> in, final Out<String> out) { in.onMessage(new F.Callback<String>() { public void invoke(String message) throws Throwable { out.write(message); } }); } }; } $ -> ws = new WebSocket("ws://localhost:9000/echo") ws.onopen = () -> ws.send("foo") ws.onmessage = (message) -> console.log(message.data)