如何将View中的参数传递给Lift中的代码片段?

问题描述:

我将使用Exploring Lift一书中稍微修改的清单4.7来问我的问题。如何将View中的参数传递给Lift中的代码片段?

// In Boot.boot: 
LiftRules.viewDispatch.append { 
    case List("Expenses", "recent", acctId, authToken) => 
     Left(() => Full(RSSView.recent(acctId, authToken))) 

    // This is a dispatch via the same LiftView object. The path 
    // "/Site/news" will match this dispatch because of our dispatch 
    // method defined in RSSView. The path "/Site/stuff/news" will not 
    // match because the dispatch will be attempted on List("Site","stuff") 
    case List("Site") => Right(RSSView) 
} 

// Define the View object: 
object RSSView extends LiftView { 
    def dispatch = { 
     case "news" => siteNews 
    } 

    def recent(acctId : String, authToken : String)() : NodeSeq = { 
     // User auth, account retrieval here 
     ... 
     <lift:surround with="rss" at="content"> 
      <lift:Vote.image /> 
     </lift:surround> 
    } 

    // Display a general RSS feed for the entire site 
    def siteNews() : NodeSeq = { ... } 
} 

我如何通过acctId从视图功能最近入段电梯:Vote.image?谢谢。

如果您尝试从用户启动时获取acctId和authToekn,则这不起作用。引导仅在Web应用程序启动时运行,而不是每个用户都运行一次。

您必须在用户登录时或者在检测到自动登录Cookie后设置SessionVar,然后在需要时访问sessionVar。

+0

我不想从启动获取acctId和authToken。我想从url中获取acctId,以便我可以渲染不同的内容。但是,SessionVar确实有助于达到这个目的。谢谢。 – coolsuntraveler 2011-12-22 07:43:48