Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Converted JavaActions.md

  • Loading branch information...
commit a4d399493ea3526a3e719f4dcb603dfca4660356 1 parent 0e3956d
@jroper jroper authored
View
47 documentation/manual/javaGuide/main/http/JavaActions.md
@@ -6,11 +6,7 @@ Most of the requests received by a Play application are handled by an `Action`.
An action is basically a Java method that processes the request parameters, and produces a result to be sent to the client.
-```java
-public static Result index() {
- return ok("Got request " + request() + "!");
-}
-```
+@[simple-action](code/javaguide/http/JavaActions.java)
An action returns a `play.mvc.Result` value, representing the HTTP response to send to the web client. In this example `ok` constructs a **200 OK** response containing a **text/plain** response body.
@@ -18,21 +14,13 @@ An action returns a `play.mvc.Result` value, representing the HTTP response to s
A controller is nothing more than a class extending `play.mvc.Controller` that groups several action methods.
-The simplest syntax for defining an action is a static method with no parameters that returns a `Result` value:
+@[full-controller](code/javaguide/http/full/Application.java)
-```java
-public static Result index() {
- return ok("It works!");
-}
-```
+The simplest syntax for defining an action is a static method with no parameters that returns a `Result` value, as shown above.
An action method can also have parameters:
-```java
-public static Result index(String name) {
- return ok("Hello" + name);
-}
-```
+@[params-action](code/javaguide/http/JavaActions.java)
These parameters will be resolved by the `Router` and will be filled with values from the request URL. The parameter values can be extracted from either the URL path or the URL query string.
@@ -42,22 +30,11 @@ Let’s start with simple results: an HTTP result with a status code, a set of H
These results are defined by `play.mvc.Result`, and the `play.mvc.Results` class provides several helpers to produce standard HTTP results, such as the `ok` method we used in the previous section:
-```java
-public static Result index() {
- return ok("Hello world!");
-}
-```
+@[simple-result](code/javaguide/http/JavaActions.java)
Here are several examples that create various results:
-```java
-Result ok = ok("Hello world!");
-Result notFound = notFound();
-Result pageNotFound = notFound("<h1>Page not found</h1>").as("text/html");
-Result badRequest = badRequest(views.html.form.render(formWithErrors));
-Result oops = internalServerError("Oops");
-Result anyStatus = status(488, "Strange response type");
-```
+@[other-results](code/javaguide/http/JavaActions.java)
All of these helpers can be found in the `play.mvc.Results` class.
@@ -67,19 +44,11 @@ Redirecting the browser to a new URL is just another kind of simple result. Howe
There are several helpers available to create redirect results:
-```java
-public static Result index() {
- return redirect("/user/home");
-}
-```
+@[redirect-action](code/javaguide/http/JavaActions.java)
The default is to use a `303 SEE_OTHER` response type, but you can also specify a more specific status code:
-```java
-public static Result index() {
- return temporaryRedirect("/user/home");
-}
-```
+@[temporary-redirect-action](code/javaguide/http/JavaActions.java)
> **Next:** [[HTTP Routing | JavaRouting]]
View
138 documentation/manual/javaGuide/main/http/code/javaguide/http/JavaActions.java
@@ -0,0 +1,138 @@
+package javaguide.http;
+
+import org.junit.Test;
+import play.mvc.Controller;
+import play.mvc.Result;
+import play.test.WithApplication;
+import play.test.Helpers;
+
+import static org.hamcrest.CoreMatchers.*;
+import static org.junit.Assert.*;
+import static play.test.Helpers.*;
+import static javaguide.http.MockJavaAction.call;
+
+public class JavaActions extends WithApplication {
+ @Test
+ public void simpleAction() {
+ assertThat(status(call(new Controller1(), fakeRequest())), equalTo(200));
+ }
+
+ static class Controller1 extends MockJavaAction {
+ //#simple-action
+ public static Result index() {
+ return ok("Got request " + request() + "!");
+ }
+ //#simple-action
+ }
+
+ @Test
+ public void fullController() {
+ assertThat(status(call(new Controller2(), fakeRequest())), equalTo(200));
+ }
+
+ static class Controller2 extends MockJavaAction {
+ public static Result index() {
+ return javaguide.http.full.Application.index();
+ }
+ }
+
+ @Test
+ public void withParams() {
+ Result result = call(new Controller3(), fakeRequest());
+ assertThat(status(result), equalTo(200));
+ assertThat(contentAsString(result), equalTo("Hello world"));
+ }
+
+ static class Controller3 extends MockJavaAction {
+ //#params-action
+ public static Result index(String name) {
+ return ok("Hello " + name);
+ }
+ //#params-action
+
+ public Result invocation() {
+ return index("world");
+ }
+ }
+
+ @Test
+ public void simpleResult() {
+ assertThat(status(call(new Controller4(), fakeRequest())), equalTo(200));
+ }
+
+ static class Controller4 extends MockJavaAction {
+ //#simple-result
+ public static Result index() {
+ return ok("Hello world!");
+ }
+ //#simple-result
+ }
+
+ @Test
+ public void otherResults() {
+ // Mock the existence of a view...
+ class Form {
+ String render(Object o) {
+ return "";
+ }
+ }
+ class Html {
+ Form form = new Form();
+ }
+ class Views {
+ Html html = new Html();
+ }
+ final Views views = new Views();
+
+ class Controller5 extends Controller {
+ void run() {
+ Object formWithErrors = null;
+
+ //#other-results
+ Result ok = ok("Hello world!");
+ Result notFound = notFound();
+ Result pageNotFound = notFound("<h1>Page not found</h1>").as("text/html");
+ Result badRequest = badRequest(views.html.form.render(formWithErrors));
+ Result oops = internalServerError("Oops");
+ Result anyStatus = status(488, "Strange response type");
+ //#other-results
+
+ assertThat(Helpers.status(anyStatus), equalTo(488));
+ }
+ }
+
+ new Controller5().run();
+ }
+
+ @Test
+ public void redirectAction() {
+ Result result = call(new Controller6(), fakeRequest());
+ assertThat(status(result), equalTo(SEE_OTHER));
+ assertThat(header(LOCATION, result), equalTo("/user/home"));
+ }
+
+ static class Controller6 extends MockJavaAction {
+ //#redirect-action
+ public static Result index() {
+ return redirect("/user/home");
+ }
+ //#redirect-action
+ }
+
+ @Test
+ public void temporaryRedirectAction() {
+ Result result = call(new Controller7(), fakeRequest());
+ assertThat(status(result), equalTo(TEMPORARY_REDIRECT));
+ assertThat(header(LOCATION, result), equalTo("/user/home"));
+ }
+
+ static class Controller7 extends MockJavaAction {
+ //#temporary-redirect-action
+ public static Result index() {
+ return temporaryRedirect("/user/home");
+ }
+ //#temporary-redirect-action
+ }
+
+
+}
View
25 documentation/manual/javaGuide/main/http/code/javaguide/http/MockJavaAction.scala
@@ -0,0 +1,25 @@
+package javaguide.http
+
+import play.core.j.JavaAction
+import play.mvc.{Controller, Result}
+import play.test.{FakeRequest, Helpers}
+import play.api.mvc.HandlerRef
+
+abstract class MockJavaAction extends Controller with JavaAction {
+
+ def invocation = method.invoke(null).asInstanceOf[Result]
+
+ def controller = this.getClass
+
+ def method = this.getClass.getDeclaredMethods()(0)
+}
+
+object MockJavaAction {
+ def call(action: JavaAction, request: FakeRequest) = {
+ val result = action.apply(request.getWrappedRequest)
+ new Result {
+ def getWrappedResult = result
+ override def toString = result.toString
+ }
+ }
+}
View
15 documentation/manual/javaGuide/main/http/code/javaguide/http/full/Application.java
@@ -0,0 +1,15 @@
+//#full-controller
+//###replace: package controllers;
+package javaguide.http.full;
+
+import play.*;
+import play.mvc.*;
+
+public class Application extends Controller {
+
+ public static Result index() {
+ return ok("It works!");
+ }
+
+}
+//#full-controller
Please sign in to comment.
Something went wrong with that request. Please try again.