3

这是我的第一个 play 2.0 应用程序,scala 对我来说仍然很新,所以我很可能在某个地方犯了错误。我正在使用一个非常新的插件,它捆绑了 Salat 和 Casbah:https ://github.com/leon/play-salat

我已经简化并重命名了所有内容以使其通用。

我的观点(views/MyController/search.scala.html):

@(modelList:List[models.MyModel])
@main(title = "Search MyModel") {
  <table>
  @for(a <- modelList) {
    <tr><td>@a.field<td>@a.field2</li>
  } 
  </table>
}

我的控制器(controllers/MyController.scala):

package controllers

import play.api._
import play.api.mvc._
import play.api.data._
import play.api.data.Forms._
import com.mongodb.casbah.Imports._
import models._

object MyController extends Controller {
  def search = Action {
    val modelList = MyModel.all.toList;
    Ok(views.html.MyController.search(modelList))
  }
}

我的模型:(models/MyModel.scala):

package models

import play.api.Play.current
import java.util.{Date}
import com.novus.salat._
import com.mongodb.casbah.Imports._
import se.radley.plugin.mongodb._
import se.radley.plugin.mongodb.salat._

case class MyModel(
  id: ObjectId = new ObjectId,
  field: String,
  field2: String
)

object MyModel extends SalatDAO[MyModel, ObjectId](collection = getCollection("mycollection")) {
  def all = find(MongoDBObject())
}

我收到了这个错误:

ClassCastException: models.MyModel cannot be cast to models.MyModel

这对我来说没有多大意义——有人遇到过这样的事情吗?

完整的堆栈跟踪:

play.core.ActionInvoker$$anonfun$receive$1$$anon$1: Execution exception [[ClassCastException: models.MyModel cannot be cast to models.MyModel]]
at play.core.ActionInvoker$$anonfun$receive$1.apply(Invoker.scala:82) [play_2.9.1.jar:2.0]
at play.core.ActionInvoker$$anonfun$receive$1.apply(Invoker.scala:63) [play_2.9.1.jar:2.0]
at akka.actor.Actor$class.apply(Actor.scala:290) [akka-actor.jar:2.0]
at play.core.ActionInvoker.apply(Invoker.scala:61) [play_2.9.1.jar:2.0]
at akka.actor.ActorCell.invoke(ActorCell.scala:617) [akka-actor.jar:2.0]
at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:179) [akka-actor.jar:2.0]
Caused by: java.lang.ClassCastException: models.MyModel cannot be cast to models.MyModel
at views.html.MyController.search$$anonfun$apply$1.apply(search.template.scala:25) ~[classes/:na]
at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:194) ~[scala-library.jar:0.11.2]
at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:194) ~[scala-library.jar:0.11.2]
at scala.collection.LinearSeqOptimized$class.foreach(LinearSeqOptimized.scala:59) ~[scala-library.jar:0.11.2]
at scala.collection.immutable.List.foreach(List.scala:45) ~[scala-library.jar:0.11.2]
at scala.collection.TraversableLike$class.map(TraversableLike.scala:194) ~[scala-library.jar:0.11.2]
4

1 回答 1

3

我认为这里的问题是您的 MyModel 集合对象

object MyModel extends SalatDAO[MyModel, ObjectId](collection = getCollection("mycollection")) {
  def all = find(MongoDBObject())
}

是在您的模板中导入的内容。

我会尝试以下方法:

package models

import play.api.Play.current
import java.util.{Date}
import com.novus.salat._
import com.mongodb.casbah.Imports._
import se.radley.plugin.mongodb._
import se.radley.plugin.mongodb.salat._

case class MyModel(
  id: ObjectId = new ObjectId,
  field: String,
  field2: String
)

object MyModelDAO extends SalatDAO[MyModel, ObjectId](collection = getCollection("mycollection")) {
  def all = find(MongoDBObject())
}

案例类已经带有伴随对象。在这种情况下MyModel,scala 会为您生成一个伴随类。扩展正确类型的对象SalatDAO也被命名为MyModel. 您将不得不查看为案例类和MyModel extends SalatDAO[MyModel, ObjectId]类生成的字节码以找出生成的类名是什么,我认为这将models.MyObject$适用于您的案例类。但是,如果你以不同的方式命名它,那么你应该得到你正在寻找的结果。

于 2012-04-12T23:33:34.657 回答