【playframework2笔记整理】1、routes和基础内容

本文来自:http://fair-jm.iteye.com/ 

转截请注明出处

 

play的请求处理:

请求==>选择Route==>绑定(进行类型转换) ==>调用Action

 

routers文件基本:

GET /      controllers.Application.home()
GET /products    controllers.Products.list(page: Int ?= 1)
GET /product/:ean   controllers.Products.details(ean: Long)
GET /product/:ean/edit  controllers.Products.edit(ean: Long)
PUT /product/:ean   controllers.Products.update(ean: Long)

 

:参数的写法是不能包括/的
用*包括/

GET /photo/*file controllers.Media.photo(file: String)

这样

/photo/5010255079763.jpg
/photo/customer-submissions/5010255079763/42.jpg
/photo/customer-submissions/5010255079763/43.jpg

都可以匹配上
routes可以包含其他的routes文件
例如有个其他的routes文件叫做barcode.routes可以在routes里写上: 加一个前缀

->   /barcode      barcode.Routes

 


增加静态文件映射
Play中用Assets增加其他的映射路径 例如增加一个html/file的:

GET     /html/:file                 controllers.Assets.at(path="/public/html", file)
GET     /assets/*file               controllers.Assets.at(path="/public", file)

 

这样main.scala.html中如果写:
@routes.Assets.at("stylesheets/main.css") 就会报错
这是因为生成的反转Assets中已经有两个path的可能是 要写成@routes.Assets.at("/public","stylesheets/main.css")
原因:

class ReverseAssets {
// @LINE:11
// @LINE:9
def at(path:String, file:String): Call = {
   (path: @unchecked, file: @unchecked) match {
// @LINE:9
case (path, file) if path == "/public/html" => Call("GET", _prefix + { _defaultPrefix } + "html/" + implicitly[PathBindable[String]].unbind("file", dynamicString(file)))                                                       
// @LINE:11
case (path, file) if path == "/public" => Call("GET", _prefix + { _defaultPrefix } + "assets/" + implicitly[PathBindable[String]].unbind("file", file))                                                      
   }
}   
}

 


匹配正则:

GET /product/:ean controllers.Products.details(ean: Long)
GET /product/:alias controllers.Products.alias(alias: String)

这样的写法是错误的 第二个不可达 总是会到达第一个如果是String类型的就会匹配出现转型异常
可以使用正则:

GET /product/$ean<\d{13}> controllers.Products.details(ean: Long)
GET /product/:alias controllers.Products.alias(alias: String)

 


绑定的环节可以自己定义

REDIRECT-AFTER-POST
HTTP/1.1 302 Found
Location: http://localhost:9000/products


routing反转(原来是URL->Action 代码中可以由Action->URL):
Redirect(routes.Products.list()) 这里就用到了routing反转
routes.Products.list()-->controllers.ReverseProducts-->Call("GET","/products")
别忘了一个Action可是会被多个URL对应的 在反转时匹配第一个出现的URL
play会自动生成反转的类 controllers.routes.类名.方法名
例如:

val call = controllers.routes.Products.list()

这只对controllers包里的有效 不在controllers包内的Controller 写完整的包.类名
 


Action返回二进制数据
用Ok(Byte数组).as(MIME类型)就行了


状态行 头部 内容
状态红用Status()返回 用withHeaders带上头部 比如:
Redirect(url)是由:Status(FOUND).withHeaders(LOCATION -> url) 实现的
Ok(json).withHeaders(CONTENT_TYPE -> "application/json")更简单的写法是:
Ok("""{ "status": "success" }""").as("application/json")

 

使用session data保存内容:

Ok(results).withSession(
request.session + ("search.previous" -> query)
)

session是Map[String,String]的形式
session是通过HTTP session cookie实现的 大小有限不要存放太大的数据(只有4KB)

 


使用Flash Scope为redirect传递数据:
Redirect(routes.Products.flash()).flashing(
"info" -> "Product deleted!"
)
获取用:
val message = request.flash("info")


 

 

静态资源:
获取还是使用反转routing比较方便

<link href="@routes.Assets.at("images/favicon.png")"
rel="shortcut icon" type="image/png">

Assets是个默认就有的控制器 在routes文件中可以看到
静态资源的压缩:
在request中 客服端在请求头上写明支持gzip 例如:Accept-Encoding:
gzip
服务器如果支持就会发送压缩的数据 并在回复头上写着:Content-Encoding: gzip
Play返回有几个条件:
1、在生产模式下(开发模式不开启)
2、请求资源是Assets控制器管理的
3、客服端允许(Accept-Encoding)Gzip
4、在静态文件目录下有和请求的资源相同名但后缀是.gz的文件

 

 

 

本笔记是个人整理所得 陆续部分会整理好之后放出

如要转截 请注明作者:fair_jm 

笔记如有错误欢迎指正^_^

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值