Play Framework 框架 控制器(Controller).docx

上传人:hs****ma 文档编号:556067432 上传时间:2023-05-16 格式:DOCX 页数:21 大小:29KB
返回 下载 相关 举报
Play Framework 框架 控制器(Controller).docx_第1页
第1页 / 共21页
Play Framework 框架 控制器(Controller).docx_第2页
第2页 / 共21页
Play Framework 框架 控制器(Controller).docx_第3页
第3页 / 共21页
Play Framework 框架 控制器(Controller).docx_第4页
第4页 / 共21页
Play Framework 框架 控制器(Controller).docx_第5页
第5页 / 共21页
点击查看更多>>
资源描述

《Play Framework 框架 控制器(Controller).docx》由会员分享,可在线阅读,更多相关《Play Framework 框架 控制器(Controller).docx(21页珍藏版)》请在金锄头文库上搜索。

1、Play Framework 框架的控制器(Controller)Business logic is managed in the domain model layer. As a client (typically a web browser) cannot directly invoke this code, the functionality of a domain object is exposed as resources represented by URIs.业务逻辑是在域模型层里进行管理,客户端(典型的客户端就是浏览器)无法直接调用业务逻辑代码,客户端是通过资源的URI来访问到

2、域对象。A client uses the uniform API provided by the HTTP protocol to manipulate these resources, and by implication the underlying business logic. However, this mapping of resources to domain objects is not a bijection: the granularity can be expressed at different levels, some resources may be virtua

3、ls, for some resources aliases may be defined客户端使用 HTTP 协议中提供的统一方法来访问这些特定资源,并隐式调用底层的业务逻辑。但是这种URI资源到域对象之间的映射关系并不是双向的,其粒度可以使用不同的层次来表示。某些资源可以是虚拟的,也可以给资源定义一个别名This is precisely the role played by the Controller layer: providing a glue between the domain model objects and transport layer events. As the M

4、odel layer, controllers are written in pure Java, making it easy to access or modify Model objects. Like the HTTP interface, Controllers are procedural and Request/Response oriented.这正是控制器层所起的作用:提供一个域模型对象与传输层之间的映射关系。由于域模型层和控制器都是纯Java编写的,因此很容易访问或修改模型对象。与HTTP接口类似,控制器是面向过程和请求/响应模型的。The Controller layer

5、 reduces the impedance mismatch between HTTP and the Domain Model.控制器层可减少由于 HTTP 协议和域模型之间不匹配的障碍。Note注意There are different architectural models with different strategies. Some protocols give you direct access to the domain model objects. This is typically what EJB or Corba protocols do. In these case

6、s, the architectural style used is RPC (Remote Procedure Call). These communication styles are hardly compatible with web architecture.不同的体系架构有着不同的设计策略,某些协议可以让你直接访问域模型对象,例如 EJB 和 CORBA 协议就是这么做的。在这种情况下,使用的 RPC 远程过程调用的设计风格,这种设计风格很难跟 Web 应用兼容。Some technologies like SOAP try to give access to the model

7、object domain through the Web. However, SOAP is just another RPC-style protocol, in this case using HTTP as a transport protocol. It is not an application protocol.而另外一些技术例如 SOAP 试图通过 Web 来访问域对象模型,但不管怎样,SOAP 还是 RPC 风格的设计,尽管使用的是 HTTP 传输协议,这并不是应用的协议。 The webs principles are not fundamentally object-or

8、iented. So a layer is needed to adapt HTTP to your favorite language.从根本上来说,Web 的原则并不是面向对象的,因此需要引入一个用来将 HTTP 协议转化为你喜好的编程语言的层,这就是控制层。A controller overview控制器概述A Controller is a Java class, hosted by the controllers package, and subclassing play.mvc.Controller.在 Play 框架中,控制器其实就是一个 Java 类,位于 controller

9、s 包中,继承了父类 play.mvc.Controller。This is a Controller:这里是一个简单控制器的源码:package controllers;import models.Client;import play.mvc.Controller;public class Clients extends Controller public static void show(Long id) Client client = Client.findById(id); render(client); public static void delete(Long id) Clien

10、t client = Client.findById(id); client.delete(); Each public, static method in a Controller is called an action. The signature for an action method is always:在这个示例控制器中,每一个被声明为 public static 的方法被称为 Action,每个 Action 的形式如下所示:public static void action_name(params.);You can define parameters in the actio

11、n method signature. These parameters will be automatically resolved by the framework from the corresponding HTTP parameters.你可以为 Action 定义各种参数,这些参数会自动与 HTTP 的参数进行绑定和赋值。Usually, an action method doesnt include a return statement. The method exit is done by the invocation of a result method. In this e

12、xample, render() is a result method that executes and displays a template.一般情况下,Action 方法无需返回任何值,该方法通过调用一个结果方法来结束执行,例如 render 就是用来执行并显示一个页面模板。Retrieving HTTP parameters获取 HTTP 参数An HTTP request contains data. This data can be extracted from: * The URI path: in /clients/1541, 1541 is dynamic part of

13、the URI Pattern. * The Query String: /clients?id=1541. * The request body: if the request was sent from an HTML form, the request body contains the form data encoded as x-www-urlform-encoded.在 HTTP 的请求中包含各种参数数据,例如: * URI路径: /clients/1541, 1541 是一种动态的URI路径模式 * Query String: /clients?id=1541. * POST方式

14、的请求,该请求来自某个表单的提交,数据编码方式x-www-urlform-encoded.In all cases, Play extracts this data and builds a Map which contains all the HTTP parameters. The key is the parameter name. The parameter name is derived from: * The name of the dynamic part of the URI (as specified in the route) * The name portion of a

15、 name-value pair taken from the Query String * The contents of a x-www-urlform-encoded body.在所有的这些情况中,Play 框架解析出这些数据,并将数据保存在一个 Map 类型的变量中,Map 的 key 就是参数名,而参数名来自于: * routes 中定义的名称,针对于例如 /clients/1541 这种URI请求 * Query String 中的参数名 * 来自x-www-urlform-encoded 内容的参数名Using the params map使用参数映射The params object is available to any Controller class (it is defined in the play.mvc.Controller super class). This object contains all the HTTP parameters fou

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 大杂烩/其它

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号