首页 文章

Play2,MongoDB,play-salat:类强制转换异常

提问于
浏览
3

我有一个对象 Foo ,其中包含我使用play-salat插件从MongoDb获取的对象列表 Bar . 模型看起来像这样 .

case class Foo (
  @Key("_id") id: ObjectId = new ObjectId,
  bars: Option[List[Bar]] = None
)

case class Bar (
  something: String
)

该视图应显示foo对象的列表 . 我通过这样的迭代器

@(foos: Iterator[Foo])

显示数据的模板部分如下所示:

@foos.map { foo =>
  <div class="foo">@foo.id</div>
  @if(foo.bars != None) {
    <ul>
      @for( bar <- bars ) {
        <li>@bar.something</li>
      }
    </ul>
  }    
}

这样做,我得到一个ClassCastException:

[ClassCastException: com.mongodb.BasicDBList cannot be cast to scala.collection.immutable.List]

我尝试过这样的其他变体

@for( i <- 0 to foo.bars.size - 1 ) {
  <li>@foo.bars.get(i).something</li>
}

导致ClassCastException:

[ClassCastException: com.mongodb.BasicDBList cannot be cast to scala.collection.LinearSeqOptimized]

问题是,我如何遍历mongodb对象列表?我想/希望某种转移对象不是必需的 .

2 回答

  • 4

    请注意salat wiki它不支持包含集合的选项 .

    尝试改为:

    case class Foo (
      @Key("_id") id: ObjectId = new ObjectId,
      bars: List[Bar] = List()
    )
    
  • 2

    Salat目前不支持包含集合的选项,即 Option[List[T]] . 有关更多信息,请参见此处:https://github.com/novus/salat/wiki/SupportedTypes

    只需使用 List ,并模拟"nothing",只需使用 List.empty[Bar] 进行初始化即可 .

相关问题