大东哥已经翻译了面向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"); }