liftweb:获取模型字段

发布于 2024-09-13 23:51:04 字数 1314 浏览 7 评论 0原文

我是 liftweb 和 scala 的新手。 我正在为 rss 聚合器开发 json-rest api,我有两个问题:

package my.domain

import net.liftweb.http._
import net.liftweb.http.rest._
import net.liftweb.json.JsonAST._
import net.liftweb.common.{Box,Full,Empty,Failure,ParamFailure}


import my.domain.model.{RssItem}

object ContentRest extends RestHelper {
 def getFirstRssItem = {
  val item = RssItem.find(ByField(RssItem.title, "test"))
  item.title
 }
 serve {
     case "api" :: "static" :: _ XmlGet _=> <b>Static</b>
     case "api" :: "static" :: _ JsonGet _ => JString("string")
 }
}

getFirstRssItem 方法的第一行和第二行都出现错误:

首先是编译器找不到 ByField 方法 - 我需要导入什么?

其次,编译器表示无法在 item val 中找到方法标题。根据 liftweb wiki,我可以将字段的名称称为方法,但 item 的类型为 Box[my.domain.model.RssItem]。我做错了什么? RssItem 型号:

package my.domain.model

import net.liftweb.mapper._

class RssItem extends KeyedMapper[Long, RssItem] {

    def getSingleton = RssItem

    def primaryKeyField = id
    object id extends MappedLongIndex(this)
    object title extends MappedString(this, 255)
    object description extends MappedText(this)
    object pubDate extends MappedDateTime(this)
}

object RssItem extends RssItem with KeyedMetaMapper[Long, RssItem] {
    def dbTable = "items"
}

I am new with liftweb and scala.
I am developing json-rest api for rss agregator and I have two problems:

package my.domain

import net.liftweb.http._
import net.liftweb.http.rest._
import net.liftweb.json.JsonAST._
import net.liftweb.common.{Box,Full,Empty,Failure,ParamFailure}


import my.domain.model.{RssItem}

object ContentRest extends RestHelper {
 def getFirstRssItem = {
  val item = RssItem.find(ByField(RssItem.title, "test"))
  item.title
 }
 serve {
     case "api" :: "static" :: _ XmlGet _=> <b>Static</b>
     case "api" :: "static" :: _ JsonGet _ => JString("string")
 }
}

I get errors on both first and second lines of getFirstRssItem method:

First is that compiler can't find ByField method - what i need to import?

Second is that compiler says it can't find method title in item val. According to liftweb wiki I may call field's name as method but item has the type Box[my.domain.model.RssItem]. What am I doing wrong?
RssItem model:

package my.domain.model

import net.liftweb.mapper._

class RssItem extends KeyedMapper[Long, RssItem] {

    def getSingleton = RssItem

    def primaryKeyField = id
    object id extends MappedLongIndex(this)
    object title extends MappedString(this, 255)
    object description extends MappedText(this)
    object pubDate extends MappedDateTime(this)
}

object RssItem extends RssItem with KeyedMetaMapper[Long, RssItem] {
    def dbTable = "items"
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寂寞清仓 2024-09-20 23:51:04

正如 Debilski 指出的那样,find() 返回一个 Box[RssItem],如果没有项目,则返回 Empty;如果找到项目,则返回 Full(item),因此获取标题需要使用 map() 或使用 for 理解(即map/flatMap/filter 上的语法糖)。

就您的查询而言,您需要 By() 而不是 ByField()。我已经清理了你的代码,以便它可以编译:

object ContentRest extends RestHelper {
  def getFirstRssItemTitle = {
    for {
      item <- RssItem.find(By(RssItem.title, "test"))
    } yield item.title
  }

  serve {
    case "api" :: "static" :: _ XmlGet _=> 
    for {
      title <- getFirstRssItemTitle ?~ "No RSS data"
    } yield <b>{title}</b>
  }
}

请注意,如果数据库中没有项目,那么你将返回 404,正文中包含“无 RSS 数据”(而不是空指针异常。)

我希望如此有帮助。

As Debilski points out, find() return a Box[RssItem] which is Empty if there are no items or Full(item) if an item was found, so getting the title requires a map() or using the for comprehension (which is syntactic sugar on map/flatMap/filter).

In terms of your query, you want By() rather than ByField(). I've cleaned up your code so it compiles:

object ContentRest extends RestHelper {
  def getFirstRssItemTitle = {
    for {
      item <- RssItem.find(By(RssItem.title, "test"))
    } yield item.title
  }

  serve {
    case "api" :: "static" :: _ XmlGet _=> 
    for {
      title <- getFirstRssItemTitle ?~ "No RSS data"
    } yield <b>{title}</b>
  }
}

Note that if there are no items in the database, then you'll return a 404 with "No RSS data" in the body (rather than a null pointer exception.)

I hope this helps.

烟织青萝梦 2024-09-20 23:51:04

想知道您是否可以尝试使用 net.liftweb.mapper.By (而不是 ByField),例如...

导入net.liftweb.mapper.By

val item = RssItem.find(By(RssItem.title, "测试")

Wondering whether you can try using net.liftweb.mapper.By (instead of ByField), something like ...

import net.liftweb.mapper.By

val item = RssItem.find(By(RssItem.title, "test")

满身野味 2024-09-20 23:51:04

Find 返回一个 Box(您可以像 Option 一样使用它),因为不清楚是否可以找到实际元素。

您可以编写类似

item.map(_.title) openOr "untitled"

item.map(_.title) 的内容,而不是 item.title ,为您提供一个标题的 Box ,其中包含标题 或 为空(如果未找到任何项目)。如果没有 Box,则会出现 Null 错误。 openOr 然后返回 Box 的内容或指定的默认值。

Find returns a Box, (which you can use just like Option,) because it is not clear whether an actual element can be found.

Instead of item.title, you would write something like

item.map(_.title) openOr "untitled"

item.map(_.title) gives you a Box of the title which either contains the title or is empty (in case no item was found). Without a Box, you’d have a Null error there. openOr then either returns the contents of the Box or specified default value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文