play2.0文档-面向java开发者(2)
2012-04-06 13:17
549 查看
HTTProuting
Thebuilt-inHTTProuter(内置的HTTProuter)
TherouteristhecomponentthattranslateseachincomingHTTPrequesttoanactioncall(astatic,publicmethodinacontrollerclass).router组件的功能是把收到的HTTPrequest转换成对action的调用(controller里的一个static,public方法).
AnHTTPrequestisseenasaneventbytheMVCframework.Thiseventcontainstwomajorpiecesofinformation:
一个HTTP请求被MVC框架看作是一个事件,这个事件包含两个主要的信息:
therequestpath(suchas
theHTTPmethod(GET,POST,…).
请求路径(例如
HTTP方法(GET,POST,...).
Routesaredefinedinthe
Routes是在
Theroutesfilesyntax(routes文件语法)
Let’sseewhataroutedefinitionlookslike:
来看一下route是怎样定义的:
Notethatintheactioncall,theparametertypecomesaftertheparametername,likeinScala.
注意在对action的调用中,参数类型在参数名称的后面,这有点像scala的定义方式。
EachroutestartswiththeHTTPmethod,followedbytheURIpattern.Thelastelementofarouteisthecalldefinition.
每个route的开头是HTTP方法,后面跟着URIpattern,最后面定义了会调用哪个action。
Youcanalsoaddcommentstotheroutefile,withthe
你也可以在route文件中添加评论,用
TheHTTPmethod
TheHTTPmethodcanbeanyofthevalidmethodssupportedbyHTTP(HTTP方法可以是HTTP支持的任何一种有效的方法(
TheURIpattern
TheURIpatterndefinestheroute’srequestpath.Somepartsoftherequestpathcanbedynamic.URIpattern表示route的请求路径.这个请求路径的某些部分可以是动态的。
Staticpath(静态路径)
Forexample,toexactlymatch例如,为了提取与
Dynamicparts(动态部分)
Ifyouwanttodefinearoutethat,say,retrievesaclientbyid,youneedtoaddadynamicpart:如果你想定义一个通过id来获取一个client的路由,你就需要添加一个动态的部分:
NotethataURIpatternmayhavemorethanonedynamicpart.
注意一个URIpattern可以有不止一个动态部分.
Thedefaultmatchingstrategyforadynamicpartisdefinedbytheregularexpression
对于动态部分的默认匹配策略是由正则表达式
Dynamicpartsspanningseveral/(跨越多个/的动态部分)
IfyouwantadynamicparttocapturemorethanoneURIpathsegment,separatedbyforwardslashes,youcandefineadynamicpartusingthe如果你想用一个动态部分来捕获多个独立的URI路径段,你可以使用正则表达式的
Here,forarequestlike
对于一个像
Dynamicpartswithcustomregularexpressions
自定义的正则表达式动态部分Youcanalsodefineyourownregularexpressionforadynamicpart,usingthe
你也可以使用
Calltoactiongeneratormethod
调用action的产生器方法Thelastpartofaroutedefinitionisthecall.Thispartmustdefineavalidcalltoanactionmethod.
route定义的最后一部分是调用。这部分必须定义一个有效的对action方法的调用。
Ifthemethoddoesnotdefineanyparameters,justgivethefully-qualifiedmethodname:
如果方法没有定义任何参数,就只需给出一个合法的方法名:
Iftheactionmethoddefinesparameters,thecorrespondingparametervalueswillbesearchedforintherequestURI,eitherextractedfromtheURIpathitself,orfromthequerystring.
如果action方法定义了参数,相对应的参数值需要从请求的URI中寻找,或者从URI路径本身提取,或者从查询字符串中提取。
Or:
Hereisthecorresponding
这是一个在
Parametertypes(参数类型)
Forparametersoftype对于String类型,参数类型是可选的。如果你想把收到的参数转换成指定的scala类型,你可以显示的添加上。
Thenusethesametypeforthecorrespondingactionmethodparameterinthecontroller:
并且在controller中相应的action方法参数也需要使用相同的类型。
Note:Theparametertypesarespecifiedusingasuffixsyntax.AlsoThegenerictypesarespecifiedusingthe
注意:参数类型是使用后缀语法定义的,泛型是使用[]符号定义,而不是像java一样使用<>定义。例如,
List[String]和Java
Parameterswithfixedvalues(带有固定值的参数)
Sometimesyou’llwanttouseafixedvalueforaparameter:有时候你可能想使用固定值作为参数:
Parameterswithdefaultvalues(带有默认值的参数)
Youcanalsoprovideadefaultvaluethatwillbeusedifnovalueisfoundintheincomingrequest:你也可以提供一个默认值,如果收到的请求中没找到参数的值就会使用这个默认值。
Routingpriority(路由优先级)
Manyroutescanmatchthesamerequest.Ifthereisaconflict,thefirstroute(indeclarationorder)isused.许多routes可以匹配相同的请求,如果有冲突的话就会使用声明中的第一个route。
Reverserouting(反转路由)
TheroutercanbeusedtogenerateaURLfromwithinaJavacall.ThismakesitpossibletocentralizeallyourURIpatternsinasingleconfigurationfile,soyoucanbemoreconfidentwhenrefactoringyourapplication.router可以根据一个java调用来产生一个URL。这使得在一个配置文件里集中配置所有你的URIpattern变得可能,这样,当你做重构是就会更加自信。
Foreachcontrollerusedintheroutesfile,therouterwillgeneratea‘reversecontroller’inthe
对于routes文件中的每一个controller,router都会在routes包里生成一个'反转controller',它有相同的action方法,相同的特征,但是返回的却是一个
The
Forexample,ifyoucreateacontrollerlike:
举个例子,如果你创建一个像这样的controller:
returnok("Hello"+name+"!");
}
}
Andifyoumapitinthe
然后你在conf/routes文件中配置映射关系:
YoucanthenreversetheURLtothe
你就可以通过使用
相关文章推荐
- play2.0文档-面向java开发者(7)异步HTTP编程
- play2.0文档-面向java开发者(5)
- play2.0文档-面向java开发者(3)
- play2.0文档-面向java开发者(4)
- play2.0文档-面向java开发者(6)
- Play 2.0 文档 - 入门 -- 针对Scala开发者
- 30天了解30种技术系列---(11)Java开发者梦想的框架Play
- Play 2.0 用户指南 - 访问SQL数据库 -- 针对Scala开发者
- Python 开发者面向文档编程的正确姿势
- 给Java开发者的Play Framework(2.4)介绍 Part1:Play的优缺点以及适用场景
- Play 2.0 用户指南 - 文件上传 -- 针对Scala开发者
- 给Java开发者的Play Framework(2.4)介绍 Part1:Play的优缺点以及适用场景
- Play 2.0 用户指南 - 使用JSON库 -- 针对Scala开发者
- Play 2.0 用户指南 - 异步HTTP编程 --针对Scala开发者
- Play 2.0 用户指南 - 表单提交和验证 --针对Scala开发者
- 黑马程序员——Java基础---面向对象(面向对象,类,对象,匿名对象,封装,构造函数,构造代码块,this,static,main,帮助文档,静态代码块,单例)
- 给Java开发者的Play Framework(2.4)介绍 Part2:使用Play,Spring,JPA进行开发
- 【原创翻译】使用Hibernate持久化java 5 的枚举类型【AppFuse 2.0 文档-11】
- java学习日记_29:面向对象之JDK帮助文档的使用。08.06至07