【问题标题】:Scala playframework - object java.lang.ProcessBuilder.Redirect is not a valueScala playframework - 对象 java.lang.ProcessBuilder.Redirect 不是一个值
【发布时间】:2018-07-11 01:12:22
【问题描述】:

Image with error

书籍控制器:

val bookForm = Form(
    tuple(
      "id" -> number,
      "title" -> text,
      "price" -> number,
      "author" -> text
    )
  )


def edit(Id: Int) = Action {
    val book: Book = Book.findById(Id)
    Ok(views.html.edit())
  }


def update(Id: Int) = Action {
          implicit request =>
          val (id, title, price, author) = bookForm.bindFromRequest.get
          val book: Book = Book.findById(id)
          book.id = id
          book.title = title
          book.price = price
          book.author = author
          Redirect(routes.BooksController.index())
      }

我的编辑视图:

<html>
<head>
    <title>Form example</title>
</head>
<body>
<form method="post" autocomplete="on">
    Id: <input type="text" name="id"><br><br>
    Title: <input type="text" name="title"><br><br>
    Price:<input type="text" name="price"><br><br>
    Author: <input type="text" name="author"><br><br>
    <input type="submit">
</form>
</body>
</html>

路线:

GET     /books                      controllers.BooksController.index
GET     /books/create               controllers.BooksController.create
GET     /books/:id                  controllers.BooksController.show(id: Int)

POST    /books/create               controllers.BooksController.save

GET     /books/edit/:id             controllers.BooksController.edit(id: Int)
POST    /books/edit                 controllers.BooksController.update

错误: play.sbt.PlayExceptions$CompilationException: 编译错误[object java.lang.ProcessBuilder.Redirect is not a value] 在 play.sbt.PlayExceptions$CompilationException$.apply(PlayExceptions.scala:34) 在 play.sbt.PlayExceptions$CompilationException$.apply(PlayExceptions.scala:34) 在 scala.Option.map(Option.scala:146) 在 play.sbt.run.PlayReload$.$anonfun$taskFailureHandler$1(PlayReload.scala:33) 在 scala.Option.map(Option.scala:146) 在 play.sbt.run.PlayReload$.taskFailureHandler(PlayReload.scala:28) 在 play.sbt.run.PlayReload$.compileFailure(PlayReload.scala:24) 在 play.sbt.run.PlayReload$.$anonfun$compile$3(PlayReload.scala:51) 在 scala.util.Either$LeftProjection.map(Either.scala:573) 在 play.sbt.run.PlayReload$.compile(PlayReload.scala:51)

【问题讨论】:

  • 我在您发布的最后一个问题中回答了您的问题。如果该答案解决了您的问题,请删除此问题,因为它与您的上一个问题重复。
  • 答案没有解决我的问题

标签: scala playframework


【解决方案1】:

让我们看看Redirect的定义:

 /**
   * Generates a redirect simple result.
   *
   * @param url the URL to redirect to
   * @param status HTTP status
   */
  def Redirect(url: String, status: Int): Result = Redirect(url, Map.empty, status)

所以你的Redirect 应该是:

Redirect("/books")

更新

好的,添加错误后,您的错误似乎是因为bookForm.bindFromRequest.get。因此,如果您查看 html 中的表单和定义的表单:

id 必须是 number 类型的输入,而不是 text,以及 price

【讨论】:

  • @Michael 复制/粘贴您的完整错误,在您问题的末尾。
  • "object java.lang.ProcessBuilder.Redirect is not a value" 并标记了 Redirect。当我回到家时,我会做短屏。
  • @Michael 更新了答案。为此更改您的 html 或表单定义。
【解决方案2】:

好的,所以我的朋友解决了这个问题。我的错误在于路线。忘记加id了:

POST    /books/edit/:id            controllers.BooksController.update(id: Int)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-06
    • 1970-01-01
    • 1970-01-01
    • 2013-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多