大東哥已經翻譯了面向scala開發者的部分http://my.oschina.net/dongming/blog?catalog=153394html
我打算翻譯一下java部分,若是錯誤請予以指正,謝謝!java
Actions, Controllers and Resultsweb
Action 是什麼?瀏覽器
Most of the requests received by a Play application are handled by an Action
.mvc
一個play應用程序收到的大部分請求都會被Action處理。app
An action is basically a Java method that processes the request parameters, and produces a result to be sent to the client.ide
一個action就是一個用來處理請求參數的基本java方法,而且它還會生成一個返回到客戶端的result。oop
public static Result index() { return ok("Got request " + request() + "!"); }
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 atext/plain response body.this
一個action 返回一個play.mvc.Result
值,這個值表明了發送到web客戶端的HTTP應答。在這個例子中,ok
構造了一個200 OK 應答,這個應答包含了一個text/plain的應答體。spa
A controller is nothing more than a class extending play.mvc.Controller
that groups several action methods.
一個controller只不過是一個繼承自 play.mvc.Controller
包含了幾個action方法的類。
The simplest syntax for defining an action is a static method with no parameters that returns a Result
value:
最簡單的定義一個action的語法是一個沒有參數的靜態方法,他返回一個 Result
值:
public static Result index() { return ok("It works!"); }
An action method can also have parameters:
action方法也能夠有參數:
public static Result index(String name) { return ok("Hello" + name); }
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.
這些參數將會被 Router
解析,並經過請求的URL來填充。參數值能夠從URL路徑或者URL查詢字符串中提取。
Let’s start with simple results: an HTTP result with a status code, a set of HTTP headers and a body to be sent to the web client.
讓咱們從簡單的results開始:一個帶有狀態碼、HTTP頭,以及須要返回發送到web客戶端的body的HTTP result。
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:
這些results 是由play.mvc.Result
定義的, play.mvc.Results
類提供了一些輔助方法來生成標準的HTTP results,就像前一部分用到的 ok
方法:
public static Result index() { return ok("Hello world!"); }
Here are several examples that create various results:
這有幾個建立各類類型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");
All of these helpers can be found in the play.mvc.Results
class.
全部這些輔助方法均可以從 play.mvc.Results
類中找到。
Redirecting the browser to a new URL is just another kind of simple result. However, these result types don’t have a response body.
重定向瀏覽器到一個新的URL僅僅是另外一種的簡單result。然而,這些result類型是沒有應答體的。
There are several helpers available to create redirect results:
這有幾個用來建立重定向results的輔助方法:
public static Result index() { return redirect("/user/home"); }
The default is to use a 303 SEE_OTHER
response type, but you can also specify a more specific status code:
這個默認值通常會使用 303 SEE_OTHER
應答類型,可是你也能夠設定一個特定的狀態碼:
public static Result index() { return temporaryRedirect("/user/home"); }