hellolift学习笔记(3)

4.Request Details

这是Menu中的第二项,我们看这里面有什么东西,入口链接是/request,这是我们在Boot.scala中定义SiteMap是指定的。他的页面,模板查找过程与index一致,我们可以找到页面中用到的snippet
<lift:BlogUtil.requestDetails />

看requestDetails的实现,又是一个xml Literals,我们来看这里用到的scala代码
Request's Locale: {S.locale}

S对象(net.liftweb.http.S)在liftweb中用来保存当前请求的状态,它由框架负责创建并填充相应的信息,在这里用来获取HTTP请求的locale值。(更详细的解释可以查看《The Definitive Guide To Lift》Ch7,P134,以后应该会经常碰到这个对象)
User.currentUser.map(ignore => S.locale.toString).openOr("No User logged in.")

(对我这样的sacla初学者,这句话包含了太多的东西 :? )
1) User是object com.hellolift.model.User;

2) 方法currentUser 的定义位于User所继承的MetaMegaProtoUser,这个方法实现目前对我来说还太复杂,在这儿先简单的知道这里将返回当前用户,返回值是一个Box[User]

i)Box (net.liftweb.util.Box) 在lift中使用的频率相当高,最近常见到几位大侠说Maybe Monad,Box就是一个这样的东西了,我理解主要目的是为了减少为避免空指针异常而导致的大量的判断代码。有一些自己的扩展,Box类似于scala中的Option,有两个子类Full和EmptyBox。
(关于Option和Box,可以进一步参考night_stalker的[url=http://night-stalker.iteye.com/blog/438684]Scala 的杂记9:Option 和 Box[/url])

ii)在这里currentUser的返回值有两种情况:当用户已登录时,返回Full(User);未登录时,返回Empty,Empty是继承了EmptyBox的Object

3) map是Box中定义的方法
def map[B](f : (A) => B) 
Apply a function to the value contained in this Box if it exists and return a new Box containing the result, or Empty otherwise
return
- the modified Box or Empty


map(ignore => S.locale.toString) 意味着,当用户已经登录时,返回Full封装的用户请求的locale值即Full(S.locale.toString);当用户未登录时,将返回Empty

4) openOr 也是Box中定义的方法

def openOr[B >: A](default : => B)
Return the value contained in this Box if it is full; otherwise return the specified default [details]
return
- the value contained in this Box if it is full; otherwise return the specified default

当用户登录时,返回S.locale.toString,当用户为登录时,返回"No User logged in."
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值