Scala Playframework-对象java.lang.ProcessBuilder.Redirect不是值

时间:2018-07-08 22:01:21

标签: scala playframework

Image with error <---当我使用函数更新时,我想重定向到BooksController.index(),但尝试时出现错误:object java.lang.ProcessBuilder.Redirect不是值。有人可以帮忙吗?

BooksController:

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:编译错误[对象java.lang.ProcessBuilder.Redirect不是值]             在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)

2 个答案:

答案 0 :(得分:0)

让我们看看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表单和定义的表单:

idnumber一样,必须是text类型而不是price的输入。

答案 1 :(得分:0)

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

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