在Play framework 2.5.12 Scala中提交表单

时间:2017-04-20 11:13:54

标签: forms scala playframework-2.0

我是新玩的框架。我正在尝试使用Play Framework 2.5.12创建一个简单的表单提交Web应用程序。我浏览了Play文档here但是在这里遇到了很多编译错误,这个文档也有些令人困惑。

  1. 玩-solr的-ZK \ build.sbt

    name := """play-solr-zk"""
    version := "1.0-SNAPSHOT"
    lazy val root = (project in file(".")).enablePlugins(PlayScala)
    scalaVersion := "2.12.1"
    
    libraryDependencies ++= Seq(
      "org.apache.solr" % "solr-solrj" % "4.10.3",
      "com.google.code.gson" % "gson" % "2.3.1"
    )
    
  2. 玩-solr的-ZK \ CONF \路由

    # An example controller showing a sample home page
    GET     /                           controllers.HomeController.index
    # An example controller showing how to use dependency injection
    GET     /count                      controllers.CountController.count
    # An example controller showing how to write asynchronous code
    GET     /message                    controllers.AsyncController.message
    
    # Map static resources from the /public folder to the /assets URL path
    GET     /assets/*file               controllers.Assets.versioned(path="/public", file: Asset)
    
  3. 玩-solr的-ZK \应用\控制器\ HomeController.scala

    package controllers
    
    import javax.inject._
    import play.api._
    import play.api.mvc._
    
    //FORMS
    import play.api.data._
    import play.api.data.Forms._
    
    import models.User
    
       @Singleton
    class HomeController @Inject() extends Controller{
    
        def index = Action {
            println("Hi!")
            Ok(views.html.index(userForm))
        }
    
    
    case class UserData(supplierID: String)
    
    val userForm = Form(
            mapping(
                    "supplierID" -> text  
                    )(UserData.apply)(UserData.unapply)
    
            )
    
    val anyData = Map("supplierID" -> "1")
    
    def userPost = Action{ implicit request =>
            userForm.bindFromRequest.fold(
            formWithErrors => {
                // binding failure, you retrieve the form containing errors:
                BadRequest(views.html.index(formWithErrors))
            },
            userData => {
                /* binding success, you get the actual value. */
                val newUser = models.User(userData.supplierID)
                val id = models.User.create(newUser)
                Redirect(routes.HomeController.index())
            }
    )
        }
    }
    
  4. 玩-solr的-ZK \应用\视图\ index.scala.html

    @(userForm: Form[UserData])(implicit messages: Messages)
    
    @import helper._
    @main("Welcome to Play") {
    
        @helper.form(action = routes.HomeController.userPost()) {
            @helper.inputText(userForm("name"))
        }
    }
    
  5. 我收到了四个错误:

    1. 此行Ok(views.html.index(userForm))。错误消息:type mismatch; found : play#37.api#18359.data#19005.Form#23899[HomeController#17830.this.UserData#248073] required: play#37.api#18359.data#19005.Form#23899[String#109465]

    2. 此行BadRequest(views.html.index(formWithErrors))。错误消息:type mismatch; found : play#37.api#18359.data#19005.Form#23899[HomeController#17830.this.UserData#248073] required: play#37.api#18359.data#19005.Form#23899[String#109465]

    3. 此行val newUser = models.User(userData.supplierID)。错误消息:object User is not a member of package models#39 Note: class User#166014 exists, but it has no companion object.

    4. 此行val id = models.User.create(newUser)。错误消息:object User is not a member of package models#39 Note: class User#166014 exists, but it has no companion object.

    5. 我只想要一个简单的表单提交。这样我就可以从我的Controller中的表单中获取数据,并可以使用这些数据进行进一步处理。请帮助我,在任何我遗漏的地方。

0 个答案:

没有答案