这是我的第一个 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]