liftweb:获取模型字段
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如 Debilski 指出的那样,find() 返回一个 Box[RssItem],如果没有项目,则返回 Empty;如果找到项目,则返回 Full(item),因此获取标题需要使用 map() 或使用 for 理解(即map/flatMap/filter 上的语法糖)。
就您的查询而言,您需要 By() 而不是 ByField()。我已经清理了你的代码,以便它可以编译:
请注意,如果数据库中没有项目,那么你将返回 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:
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.
想知道您是否可以尝试使用 net.liftweb.mapper.By (而不是 ByField),例如...
Wondering whether you can try using net.liftweb.mapper.By (instead of ByField), something like ...
Find
返回一个Box
(您可以像Option
一样使用它),因为不清楚是否可以找到实际元素。您可以编写类似
item.map(_.title)
的内容,而不是item.title
,为您提供一个标题的Box
,其中包含标题 或 为空(如果未找到任何项目)。如果没有Box
,则会出现 Null 错误。openOr
然后返回Box
的内容或指定的默认值。Find
returns aBox
, (which you can use just likeOption
,) because it is not clear whether an actual element can be found.Instead of
item.title
, you would write something likeitem.map(_.title)
gives you aBox
of the title which either contains the title or is empty (in case no item was found). Without aBox
, you’d have a Null error there.openOr
then either returns the contents of theBox
or specified default value.