首页  

play action 要点     所属分类 play 浏览量 522
https://www.playframework.com/documentation/2.8.x/ScalaActions

Actions, Controllers and Results


Controllers are action generators


A play.api.mvc.Action is basically a (play.api.mvc.Request => play.api.mvc.Result) function that handles a request and generates a result to be sent to the client. action 是一个函数 (play.api.mvc.Request => play.api.mvc.Result) def echo = Action { request => Ok("Got request [" + request + "]") } An action returns a play.api.mvc.Result value, representing the HTTP response to send to the web client. In this example Ok constructs a 200 OK response containing a text/plain response body. 响应码+响应体 Within any controller extending BaseController, the Action value is the default action builder Action { Ok("Hello world") } the Action value is the default action builder !!!!!! trait BaseController extends BaseControllerHelpers { // The default ActionBuilder. Used to construct an action, for example: // This is meant to be a replacement for the now-deprecated Action object, and can be used in the same way. def Action: ActionBuilder[Request, AnyContent] = controllerComponents.actionBuilder } Action { request => Ok("Got request [" + request + "]") } implicit 的使用 It is often useful to mark the request parameter as implicit so it can be implicitly used by other APIs that need it: Action { implicit request => Ok("Got request [" + request + "]") } implicit 变量 和 函数参数 def action = Action { implicit request => anotherMethod("Some para value") Ok("Got request [" + request + "]") } def anotherMethod(p: String)(implicit request: Request[_]) = { // do something that needs access to the request }
Controllers are action generators Controllers are typically defined as classes to take advantage of Dependency Injection. Controllers定义成 class , 方便利用 依赖注入 defining controllers as objects will not be supported in future versions of Play. Using classes is the recommended approach. 推荐 classs ,定义成object 将来可能不支持 !!!
Simple results simple results an HTTP result with a status code, a set of HTTP headers and a body to be sent to the web client play.api.mvc.Result import play.api.http.HttpEntity def index = Action { Result( header = ResponseHeader(200, Map.empty), body = HttpEntity.Strict(ByteString("Hello world!"), Some("text/plain")) ) } several examples to create various results: val ok = Ok("Hello world!") val notFound = NotFound val pageNotFound = NotFound(<h1>Page not found</h1>) val badRequest = BadRequest(views.html.form(formWithErrors)) val oops = InternalServerError("Oops") val anyStatus = Status(488)("Strange response type") All of these helpers can be found in the play.api.mvc.Results trait and companion object. Redirects are simple results too def index = Action { Redirect("/user/home") } def index = Action { Redirect("/user/home", MOVED_PERMANENTLY) } def index(name: String) = TODO TODO 页面

上一篇     下一篇
play框架调试关键代码

scala 包对象 package object

play Configuration 配置读取

play HTTP routing

play框架日志

play框架依赖注入