资源预览内容
第1页 / 共16页
第2页 / 共16页
第3页 / 共16页
第4页 / 共16页
第5页 / 共16页
第6页 / 共16页
第7页 / 共16页
第8页 / 共16页
第9页 / 共16页
第10页 / 共16页
亲,该文档总共16页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
博客园新随笔联系订阅管理随笔-35 章-0 评论-0 秦Xitrum学习笔记学习笔记03 - Action和和ViewAction:Xitrum 提供了3种Action:普通Action, FutureAction 和 ActorAction它们都是Trait,不是 Class1. 普通Action:当请求到来时,Action实现类直接在Netty的 IO线程上运,不能普通Action来执 消耗很时间的处理,否则Netty就不能接收新的连接或发出响应到客端import xitrum.Action import xitrum.annotation.GET GET(“hello“) class HelloAction extends Action def execute() respondText(“Hello“) 2. FutureAction:公告公告昵称:秦 园龄:1年2个 粉丝:2 关注:1 +加关注三三四四五五六六 262728293012 3456789 10111213141516 17181920212223 24252627282930 31123456搜索搜索找找看歌搜索常链接常链接 我的随笔 我的评论 我的参与 最新评论 我的标签Create PDF in your applications with the Pdfcrowd HTML to PDF APIPDFCROWD扩展了 Action,FutureAction实现类异步运在和ActorAction相同的线程池上,这个线程池和Netty线程池是分离的FutureAction实现类的执context是 xitrum.Config.actorSystem.dispatcher.trait FutureAction extends Actionimport xitrum.FutureAction import xitrum.annotation.GET GET(“hello“) class HelloAction extends FutureAction def execute() respondText(“hi“) 3. ActorAction:trait ActorAction extends Actor with Action实现类是个 Akka actor。当有需求时,个actor实例被创建。当连接关闭时,或者响应通过 respondText/respondView 等法被发出时,actor会被停。对于分块的响应,actor会在最后个块的响应发出后才被停。actor运在命名为“xiturm“的Akka actor系统的线程池中。我的标签我的标签 Xitrum(2)随笔分类随笔分类 AngularJS(2) hadoop HBase-1.2(8) Storm-1.1.0(6) Xitrum(18) Xitrum-Scalate-Jade(1)随笔档案随笔档案 2017年10 (1) 2017年9 (8) 2017年8 (5) 2017年7 (1) 2017年5 (20)阅读排榜阅读排榜 1. Xitrum学习笔记01 - 简介(186) 2. Xitrum学习笔记04 - RESTful APIs(141) 3. Xitrum学习笔记03 - Action和View(90) 4. Storm HBase 集成(80) 5. Xitrum学习笔记19 - 部署到产环境(75)Create PDF in your applications with the Pdfcrowd HTML to PDF APIPDFCROWD1 import scala.concurrent.duration._ 2 import xitrum.ActorAction 3 import xitrum.annotation.GET 4 GET(“actor“) 5 class HelloAction extends ActorAction 6 def execute() 7 / See Akka doc about scheduler 9 context.system.scheduler.scheduleOnce(3.seconds, self, System.currentTimeMillis() 10 / See Akka doc about “become“ 11 context.become 12 case pastTime = 13 respondInlineView(s“Its $pastTime Unix ms 3s ago.“) 14 15 16 响应响应(Response)式:式:1. 对于action来说,要向客端发出响应,可以调如下法 respondView: responds view template file, with or without layout respondInlineView: responds embedded template (not separate template file), with orwithout layout respondText(“hello“): responds a string without layout respondHtml(“.“): same as above, with content type set to “text/html” respondJson(List(1, 2, 3): converts Scala object to JSON object then responds respondJs(“myFunction(1, 2, 3)“) respondJsonP(List(1, 2, 3), “myFunction“): combination of the above two respondJsonText(“1, 2, 3“) respondJsonPText(“1, 2, 3“, “myFunction“) Create PDF in your applications with the Pdfcrowd HTML to PDF APIPDFCROWD respondBinary: responds an array of bytes respondFile: sends a file directly from disk, very fast because zero-copy (aka send-file) isused respondEventSource(“data“, “event“)2. 响应返回事先创建好的View模板件内容要运这种式,每个Action scala件都要对应个View jade件。代码例如下:scr/main/scala/mypackage/MyAction.scala:package mypackage import xitrum.Action import xitrum.annotation.GET GET(“myAction“) class MyAction extends Action def execute() respondView() def hello(what: String) = “Hello %s“.format(what) scr/main/scalate/mypackage/MyAction.jade:- import mypackage.MyAction ! 5 html Create PDF in your applications with the Pdfcrowd HTML to PDF APIPDFCROWDhead != antiCsrfMeta != xitrumCss /包含了Xitrum默认的CSS。这个CSS是可以移除的 != jsDefaults /包含了 jQuery、jQuery验证插件等等内容,要放在标签中 title Welcome to Xitrum body a(href=url) Path to the current action p= currentAction.asInstanceOfMyAction.hello(“World“) /将currentAction转换为MyAction,并调其hello法 != jsForView /包含了由jsAddToView标签添加的JavaScript代码,jsForView标签应该放在整个layout的底部在View模板中,可以使 xitrum.Action中定义的所有法,且也可以使由Scalate提供的些utility法(参考 Scalate doc)默认的 Scalate 模板类型是 Jade,其他的有 Mustache, Scaml 和 Ssp。在config/xitrum.conf件中配置默认模板类型.# Comment out if you dont use template engine. template “xitrum.view.Scalate“ defaultType = jade # jade, mustache, scaml, or ssp 也可以在Action实现类调 respondView时指定模板类型Create PDF in your applications with the Pdfcrowd HTML to PDF APIPDFCROWDval options = Map(“type“ -“mustache“) respondView(options)如果需要在View中多次调Action实现类的的法的话,只需要将currentAction做次转换,如:- val myAction = currentAction.asInstanceOfMyAction; import myAction._ p= hello(“World“) p= hello(“Scala“) p= hello(“Xitrum“)Layout利respondView或respondInlineView提交个View时,Xitrum会把View内容表成个String,然后把这个String设置给renderedView变量。Xitrum调当前Action的layout法,把法的结果返回给浏览器。如果当前Action的layout法没被调,则没有结果返回给浏览器。默认的layout法只返回 renderedView 本,可以通过重写layout法以添加关于view的其他内容。如果重写的layout法中包含renderedView,它只是作为layout的部分内容。layout法在action的view成之后才被调,layout法返回的内容就是要响应到浏览器中的内容。可以通过创建个继承Action的Trait,并将其相应的View模板件定义为通layout。再让其他Actionscala实现这个Trait,这样其他Action的就会包含这个通的layout。例:src/main/scala/mypackage/AppAction.scalapackage mypackage import xitrum.Action trait AppAction extends Action Create PDF in your applications with the Pdfcr
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号