Play2.0文档-面向JAVA开发者(1)

大东哥已经翻译了面向scala开发者的部分http://my.oschina.net/dongming/blog?catalog=153394

我打算翻译一下java部分,如果错误请予以指正,谢谢!

Actions, Controllers and Results

Action 是什么?

Most of the requests received by a Play application are handled by an Action.

一个play应用程序收到的大部分请求都会被Action处理。

An action is basically a Java method that processes the request parameters, and produces a result to be sent to the client.

一个action就是一个用来处理请求参数的基本java方法,并且它还会生成一个返回到客户端result

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.

一个action 返回一个play.mvc.Result 值,这个值代表了发送到web客户端的HTTP应答。在这个例子中,ok 构造了一个200 OK 应答,这个应答包含了一个text/plain的应答体。

Controllers

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查询字符串中提取。

Results

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 类中找到。

Redirects are simple results too

Redirects(重定向)也是简单的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"); }

------------------------------------------以下做个小广告----------------------------------------------
NoSQL技术QQ群 : 
一群:23152359(满员)   
二群:193713524(强烈推荐,即将突破400人)   
三群:79377097(新建)   
四群:191845335(新建) 
本群汇聚了包括百度、创新工厂、IBM、阿里、淘宝、京东、盛大等诸多知名企业的高端技术人才,
以各种NoSQL技术为主的大型交流群,欢迎各种猴子占领本群。

你可能感兴趣的:(Play2.0文档-面向JAVA开发者(1))