本文来自: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
笔记如有错误欢迎指正^_^
相关推荐
通过简单的代码和思路写了play framework的简单的小例子,包括添加页面进行的routes配置,页面跳转,页面之间的数据传递等方面。暂时未使用h2内置数据库及ArrayList、HashMap在页面之间的数据传递。
[强烈推荐, 文档不多, 很快就可以看完, 看完了, 就会使用play了] 目录 MVC应用程序模型 - 7 - app/controllers - 8 - app/models - 8 - app/views - 8 - 请求生命周期 - 8 - 标准应用程序布局layout - 9 - app...
所有的HTTP请求都遵循下面的处理流程: Playframework是一个无状态的面向请求/回应的框架,所有的HTTP请求都遵循下面的处理流程: 框架接收到一个HTTPRequest Router组件试图从conf/routes文件中找出对应的...
angular2-website-routes, 使用路由的简单 Angular 2网站示例 使用路由构建 Angular 2网站这是一个使用全新路由器的简单 Angular 2网站。 它演示了如何构建组件。配置路由。注入服务,以及使用 @Input decorator将...
这是 Play Framework 2.3.x 版的示例“Hello World”应用程序。 它是从转换而来的。 相关代码 使用activator new hello-world play-java创建应用程序后,唯一需要更改的内容在这些文件中: app/controllers/...
Routes is a Python re-implementation of the Rails routes system for mapping URLs to application actions, and conversely to generate URLs. Routes makes it easy to create pretty and concise URLs that ...
17.Play模块和模块仓库 - 115 - 什么是模块? - 115 - 如何从一个应用程序里加载模块 - 115 - 从模块加载默认的routes - 115 - 为模块增加文档说明 - 115 - 使用模块仓库 - 116 - 贡献新模块到模块仓库里 - 117 - ...
Play 子项目的路由附加到路由系统在modules/{subproject-name}/conf/{subproject-name}.routes 中播放子项目的路由播放子项目的build.sbt没有不必要的信息Play 子项目的控制器和视图驻留在附加子包 {subproject-name...
Laravel开发-laravel-routes Laravel上的路由分组
routes-2.6.7-10.diff.txt
官方离线安装包,亲测可用。使用rpm -ivh [rpm完整包名] 进行安装
前端开源库-react-routesReact Routes,轻量级同构HTML5路由器,用于ReactJS。
next-routes, Next.js的通用动态路由 Next.js 动态路由 易于使用通用动态路由的Next.js表达式和参数匹配面向 express &的请求处理程序中间件通过路由定义生成url的Link 和 Router:如何使用安装:npm
Laravel开发-laravel-jwt-routes 一个Laravel包,用于使用JWT Web令牌轻松实现REST API身份验证
离线安装包,亲测可用
Laravel开发-laravel-admin-routes Bytenet Laravel管理路线包,提供运输路线操作
OAuth2资源服务器库 包含用于使用Yetu oauth2provider服务器进行访问令牌验证的可重用代码。 目前(2014年10月),尚不支持Yetu组织外部的人在Yetu中注册应用的过程-但我们希望尽快对此进行更改。 另外,我们打算在...
在yarn add express-mount-routes或npm install express-mount-routes --save : const path = require ( 'path' ) ; const express = require ( 'express' ) ; const routes = require ( 'express-mount-routes' )...
Node.js产品部小样public(页面引入js文件)和routes路由和html页面
ember-routes-components 它利用了组件的优势,并允许您使用它们代替路由。 兼容性 Ember.js v2.18或更高版本 Ember CLI v2.13或更高版本 安装 ember install ember-routes-components 用法 创建带有路由名称的...