简体   繁体   English

无法从Lift Snippet访问Menu.param的参数

[英]Cannot access the parameter of a Menu.param from a Lift Snippet

I'm trying to extract the parameter from a Lift Menu.param within a snippet so that I can use it to create a named Comet. 我正在尝试从片段中的Lift Menu.param中提取参数,以便我可以使用它来创建命名的Comet。 However, I get a NullPointerException when I try to pass the parameter to the snippet using SnippetDisptach in my Boot.scala, as suggested here: http://comments.gmane.org/gmane.comp.web.lift/44299 但是,当我尝试使用我的Boot.scala中的SnippetDisptach将参数传递给代码段时,我得到一个NullPointerException,如下所示: http ://comments.gmane.org/gmane.comp.web.lift/44299

I've created the Menu item as follows: 我已经创建了Menu项,如下所示:

object AnItemPage {
// create a parameterized page
  def menu = Menu.param[Item]("Item", "Item",
                                s => fetchItem(s), item => item._id.toString) / "item"

  private def fetchItem(s:String) : Box[Item] = synchronized {
          ItemDAO.findById(ObjectId.massageToObjectId(s)) 
  }

}

I've added the menu to SiteMap. 我已将菜单添加到SiteMap。 I've also created a Snippet which I would like to pick up the Item parameter. 我还创建了一个Snippet,我想获取Item参数。 (I'm using fmpwizard's InsertNamedComet library here): (我在这里使用fmpwizard的InsertNamedComet库):

 class AddCometItemPage(boxedItem: Box[Item]) extends InsertNamedComet with DispatchSnippet{
     val item : Item = boxedItem.openOr(null)
     override lazy val name= "comet_item_" + item._id.toString
     override lazy val cometClass= "UserItemCometActor"

     def dispatch = null
    }

My next step is to crate an instance of this class as demonstrated by David Pollak here: http://comments.gmane.org/gmane.comp.web.lift/44299 我的下一步是根据David Pollak在这里展示的这个类的实例: http//comments.gmane.org/gmane.comp.web.lift/44299

This is what I have added to my Boot.scala: 这是我添加到Boot.scala中的内容:

LiftRules.snippetDispatch.append {
  case "item_page" => new AddCometItemPage(AnItemPage.menu.currentValue)
}

My item.html references this snippet: 我的item.html引用了以下代码段:

 <div class="lift:item_page">

I get the following null pointer exception when I compile and run this: 当我编译并运行它时,我得到以下空指针异常:

Exception occurred while processing /item/5114eb4044ae953cf863b786

Message: java.lang.NullPointerException
net.liftweb.sitemap.Loc$class.siteMap(Loc.scala:147)
net.liftweb.sitemap.Menu$ParamMenuable$$anon$9.siteMap(Menu.scala:170)
net.liftweb.sitemap.Loc$class.allParams(Loc.scala:123)
net.liftweb.sitemap.Menu$ParamMenuable$$anon$9.allParams(Menu.scala:170)
net.liftweb.sitemap.Loc$class.net$liftweb$sitemap$Loc$$staticValue(Loc.scala:87)
net.liftweb.sitemap.Menu$ParamMenuable$$anon$9.net$liftweb$sitemap$Loc$$staticValue(Menu.scala:170)
net.liftweb.sitemap.Loc$$anonfun$paramValue$2.apply(Loc.scala:85)
net.liftweb.sitemap.Loc$$anonfun$paramValue$2.apply(Loc.scala:85)
net.liftweb.common.EmptyBox.or(Box.scala:646)
net.liftweb.sitemap.Loc$class.paramValue(Loc.scala:85)
net.liftweb.sitemap.Menu$ParamMenuable$$anon$9.paramValue(Menu.scala:170)
net.liftweb.sitemap.Loc$$anonfun$currentValue$3.apply(Loc.scala:114)
net.liftweb.sitemap.Loc$$anonfun$currentValue$3.apply(Loc.scala:114)
net.liftweb.common.EmptyBox.or(Box.scala:646)
net.liftweb.sitemap.Loc$class.currentValue(Loc.scala:114)
net.liftweb.sitemap.Menu$ParamMenuable$$anon$9.currentValue(Menu.scala:170)
bootstrap.liftweb.Boot$$anonfun$lift$8.apply(Boot.scala:107)
bootstrap.liftweb.Boot$$anonfun$lift$8.apply(Boot.scala:106)
net.liftweb.util.NamedPF$$anonfun$applyBox$1.apply(NamedPartialFunction.scala:97)
net.liftweb.util.NamedPF$$anonfun$applyBox$1.apply(NamedPartialFunction.scala:97)
net.liftweb.common.Full.map(Box.scala:553)
net.liftweb.util.NamedPF$.applyBox(NamedPartialFunction.scala:97)
net.liftweb.http.LiftRules.snippet(LiftRules.scala:711)
net.liftweb.http.LiftSession$$anonfun$net$liftweb$http$LiftSession$$findSnippetInstance$1.apply(LiftSession.scala:1506)
net.liftweb.http.LiftSession$$anonfun$net$liftweb$http$LiftSession$$findSnippetInstance$1.apply(LiftSession.scala:1506)
net.liftweb.common.EmptyBox.or(Box.scala:646)
net.liftweb.http.LiftSession.net$liftweb$http$LiftSession$$findSnippetInstance(LiftSession.scala:1505)
net.liftweb.http.LiftSession$$anonfun$locateAndCacheSnippet$1$1$$anonfun$apply$88.apply(LiftSession.scala:1670)
net.liftweb.http.LiftSession$$anonfun$locateAndCacheSnippet$1$1$$anonfun$apply$88.apply(LiftSession.scala:1669)

Has anybody any idea where I'm going wrong? 有谁知道我哪里出错了? I've not been able to find a lot of information on Menu.param. 我无法在Menu.param上找到很多信息。

Thank you very much for your help. 非常感谢您的帮助。

f F

I have never tried what you are doing, so I am not sure the best way to accomplish it. 我从未尝试过你正在做的事情,所以我不确定实现它的最好方法。 The way you are using the Loc Param, you are extracting a variable from a URL pattern. 您使用Loc Param的方式是从URL模式中提取变量。 In your case, http://server/item/ITEMID where ITEMID is the string representation of an Item, and which is the value that gets passed to the fetchItem function. 在您的情况下, http://server/item/ITEMID其中ITEMID是Item的字符串表示形式,它是传递给fetchItem函数的值。 The function call will not have a value if you just arbitrarily call it, and from what I can see you are requesting a value that is not initialized. 如果您只是随意调用它,函数调用将没有值,并且从我可以看到您请求的值未初始化。

I would think there are two possible solutions. 我认为有两种可能的解决方案。 The first would be to use S.location instead of AnItemPage.menu.currentValue . 第一种方法是使用S.location而不是AnItemPage.menu.currentValue It will return a Box[Loc[Any]] representing the Loc that is currently being accessed (with the parameters set). 它将返回一个Box[Loc[Any]]表示当前正在访问的Loc (设置参数)。 You can use that Loc to retrive currentValue and set your parameter. 您可以使用该Loc来检索currentValue并设置参数。

The other option would be to instantiate the actor in your snippet. 另一种选择是在你的代码片段中实例化actor。 Something like this: 像这样的东西:

item.html item.html

   <div data-lift="AnItemPage">
     <div id="mycomet"></div>
   </div>

And then in your AnItemPage snippet, something like this: 然后在你的AnItemPage片段中,像这样:

class AnItemPage(item: Item) {
  def render = "#mycomet" #> new AddCometItemPage(item).render 
}

I haven't tested either of those, so they'll probably need some tweaking. 我没有测试过这些,所以他们可能需要一些调整。 Hopefully it will give you a general idea. 希望它会给你一个大致的想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM