play framework2学习之旅<2.1>Actions, Controllers and Results
来源:互联网 发布:js除以100 编辑:程序博客网 时间:2024/05/24 15:38
Actions, Controllers and Results
What is an Action?
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.
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.
Controllers
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 astaticmethod with no parameters thatreturns a Result
value:
public static Result index() { return ok("It works!");}
An action method can also have parameters:
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.
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.
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:
public static Result index() { return ok("Hello world!");}
Here are several examples that create various 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
class中查到,到时候查API即可!
Redirects are simple results too
Redirecting the browser to a new URL is just another kind of simple result. However, these result types don’t have a response body.
There are several helpers available to create redirect 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:
public static Result index() { return temporaryRedirect("/user/home");}
关于重定向,只要记住这两个方法即可,但要记得default是temporaryRedirect;
public static Result temporaryRedirect(java.lang.String url)
- play framework2学习之旅<2.1>Actions, Controllers and Results
- Actions, Controllers and Results(翻译)
- play framework2学习之旅<2.4>Session and Flash scopes
- play framework2学习之旅<1>
- play framework2学习之旅<2.2>HTTP routing
- play framework2学习之旅<2.3>Manipulating the response
- play framework2学习之旅<2.5>Body parsers
- 1.1_Actions, Controllers and Results
- ASP.NET MVC Controllers and Actions
- Change Layout in Controllers and Actions in Zend Framework 2
- Play Framework2之部署--项目打包成war包
- play 控制器层(Controllers
- play framework2开发(二)
- play framework2开发(三)
- play framework2开发(四)
- play framework2开发(五)
- play framework2开发(六)
- play framework2开发(七)
- 怎么在网页中通过JAVAscript控制SVG图像放大缩小
- Hibernate的拦截器和监听器
- 大O标记
- linux fuse 文件系统制作总结
- 使用POST方式获取页面
- play framework2学习之旅<2.1>Actions, Controllers and Results
- u-boot中代码的疑问(_armboot_start与_start?) .
- IOS学习之UINavigationController详解与使用(一)添加UIBarButtonItem
- PhpMyAdmin安装
- 模拟使用lvm快照功能进行在线热备
- c++中的string用法(二)
- centos6.2 下安装Editra笔记
- VS2010创建DLL文件的相关配置
- 深度探索C++对象模型之(六)