在我的Play Scala项目中,我想将值插入到我的数据库中。为此,我将表单字段映射到Controller
类,但在Controller
类中,它显示apply/unapply
方法为not a member of object controllers
。
控制器/ User.scala
package controllers
import play.api.mvc._
import play.api.data._
import play.api.data.Forms._
import views._
import models._
object User extends Controller {
val userForm = Form(
mapping(
"username" -> nonEmptyText,
"emailid" -> text,
"password" -> nonEmptyText
)(User.apply)(User.unapply)
)
}
模型/ User.scala
package models
import play.api.db._
import anorm._
import anorm.SqlParser._
import play.api.Play.current
import scala.language.postfixOps
case class User (
username: String,
emailid: String,
password: String
)
object User {
val simple = {
get[String]("user.USER_NAME") ~
get[String]("user.EMAIL_ID") ~
get[String]("user.PASSWORD") map {
case username~emailid~password =>
User(username, emailid, password)
}
}
}
如果我运行activator compile
命令,则显示在Exception
[error] D:\Test\app\controllers\User.scala:29: value a
pply is not a member of object controllers.User
[error] )(User.apply)(User.unapply)
[error] ^
[error] D:\Test\app\controllers\User.scala:29: value u
napply is not a member of object controllers.User
[error] )(User.apply)(User.unapply)
[error] ^
[error] two errors found
[error] (compile:compileIncremental) Compilation failed
[error] Total time: 25 s, completed Jun 3, 2015 5:44:05 PM
答案 0 :(得分:2)
这是因为,即使它们位于不同的包中,您的控制器和模型也会使用相同的名称User
。
您必须通过指定Mon Calamari在评论中指出的包(例如(models.User.apply)
)来消除歧义。
但更重要的是,您应该将控制器重命名为Users
,以避免歧义。在Play框架中,约定是复数控制器名称。