一、Action(play.api.mvc.Action)

大多数的应用的请求都是由action进行处理,并生成一个结果给客户端,Action有多种创建方式:

1.

Action {Ok("Hello world")}

2.

Action { request =>Ok("Got request [" + request + "]")}

3.

Action { implicit request =>Ok("Got request [" + request + "]")}

4.

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
}

5.

Action(parse.json) { implicit request =>Ok("Got request [" + request + "]")}

6.空的响应

def index(name:String) = TODO

二、 controller

controller通常是生成Action值的对象,通常把它定义为一个类而不是一个对象,例如:

package controllers
import javax.inject.Inject
import play.api.mvc._
class Application @Inject()(cc: ControllerComponents)extends AbstractController(cc) {
def index = Action {
Ok("It works!")
}
}

三、Result(play.api.mvc.Result)

1.带header和body的http结果

import play.api.http.HttpEntity

def index = Action {
Result(
header = ResponseHeader(200, Map.empty),
body = HttpEntity.Strict(ByteString("Hello world!"), Some("text/plain"))
)
}

其他的一些结果

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")

2.重定向到一个新的URL

def index = Action {Redirect("/user/home", MOVED_PERMANENTLY)}

四、Routes(conf/routes)

主要包含http请求方式((GETPATCHPOSTPUTDELETEHEAD)和请求地址

1.动态请求地址

GET   /clients/:id          controllers.Clients.show(id: Long)

包含多层结构的地址

GET   /files/*name          controllers.Application.download(name)

/files/images/logo.png中*name代表的是 images/logo.png

带正则表达式的地址

GET   /items/$id<[0-9]+>    controllers.Items.show(id: Long)

2.函数参数

GET   /:page                controllers.Application.show(page)
GET   /                     controllers.Application.show(page)

第二个函数的参数,是请求到的内容

例子:

def show(id: Long) = Action {
Client.findById(id).map { client =>
Ok(views.html.Clients.display(client))
}.getOrElse(NotFound)
}
GET   /clients              controllers.Clients.list(page: Int ?= 1)

函数的参数带默认值

GET   /api/list-all         controllers.Api.list(version: Option[String])

可选的参数

3.反向路由

package controllers
import javax.inject.Inject
import play.api._
import play.api.mvc._ class Application @Inject()(cc:ControllerComponents) extends AbstractController(cc) {
def hello(name: String) = Action {
Ok("Hello " + name + "!")
}
} GET /hello/:name controllers.Application.hello(name) def helloBob = Action {Redirect(routes.Application.hello("Bob"))}

4.相对路由

package controllers
import javax.inject._
import play.api.mvc._
@Singleton
class Relative @Inject()(cc: ControllerComponents) extends AbstractController(cc) {
def helloview() = Action { implicit request =>
Ok(views.html.hello("Bob"))
}
def hello(name: String) = Action {
Ok(s"Hello $name!")
}
} GET /foo/bar/hello controllers.Relative.helloview
GET /hello/:name controllers.Relative.hello(name) @(name: String)(implicit request: RequestHeader)
<h1>Hello @name</h1>
<a href="@routes.Relative.hello(name)">Absolute Link</a>
<a href="@routes.Relative.hello(name).relative">Relative Link</a> <!DOCTYPE html>
<html lang="en">
<head>
<title>Bob</title>
</head>
<body>
<a href="/hello/Bob">Absolute Link</a>
<a href="../../hello/Bob">Relative Link</a>
</body>
</html>

5.一些默认的

# Redirects to https://www.playframework.com/ with 303 See Other
GET /about controllers.Default.redirect(to = "https://www.playframework.com/") # Responds with 404 Not Found
GET /orders controllers.Default.notFound # Responds with 500 Internal Server Error
GET /clients controllers.Default.error # Responds with 501 Not Implemented
GET /posts controllers.Default.todo

最新文章

  1. sql server之临时表
  2. iOS通知NSNotificationCenter
  3. java selenium webdriver实战 seleniumIDE
  4. spark 编程向导
  5. angularjs1.6.4中使用ng-table出现data.slice is not a function的问题
  6. define 的全部使用方法
  7. iTOP-4418开发板所用核心板研发7寸/10.1寸安卓触控一体机
  8. jquery.filter() 实现元素前3个显示,其余的隐藏
  9. 在Azure DevOps Server(TFS系统)中部署回退/回滚方案(Rollback)
  10. macOS 系统 .DS_Store 文件详解
  11. centos7.2 使用rpm安装jdk8
  12. CRF(Conditional Random Field)
  13. hdu 5692(dfs+线段树) Snacks
  14. PowerDesigner设计的数据库 ORA-0092
  15. 一个还不错的gridview 样式【Z】
  16. MyBatis通用Mapper技巧
  17. [转]mii-tool与ethtool的用法详解
  18. swift - 网络请求数据处理 - 协议处理
  19. 简单的dp加贪心
  20. 使用C#压缩解压rar和zip格式文件

热门文章

  1. Spring MVC(三)--控制器接受普通请求参数
  2. mysql 如果处理货币金钱类型
  3. python-基础-字符串-列表-元祖-字典2
  4. hbase连接linux开发过程
  5. H5C3--圆角
  6. vmware 虚拟机有时候显示有网络访问,但是打不开网页的白痴解决办法
  7. hdu 1269 (强联通分量Tarjan入门)
  8. html文件中script标签放在哪里?
  9. List的深度序列化Demo
  10. 【洛谷】P1554 梦中的统计