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

Image with error <---当我使用函数更新时,我想重定向到BooksController.index()但是当我尝试它时我有一个错误:对象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)at play.sbt.PlayExceptions $ scilationException $ .apply(PlayExceptions.scala:34)at scala.Option.map(Option.scala:146)at play.sbt.run.PlayReload $ . $ anonfun $ taskFailureHandler $ 1(PlayReload.scala:33)at scala . 在play.sbt.run.PlayReload $ .taskFailureHandler(PlayReload.scala:28)的play.smap(Option.scala:146)at play.sbt.run.PlayReload $ .compileFailure(PlayReload.scala:24)at play.sbt .run.PlayReload $ . $ anonfun $ compile $ 3(PlayReload.scala:51)at scala.util.Either $ LeftProjection.map(Either.scala:573)at play.sbt.run.PlayReload $ .compile(PlayReload.scala :51)

回答(2)

2 years ago

让我们看一下 Redirect 的定义:

/ **
*生成重定向简单结果 .

  • @param url要重定向到的URL
  • @param状态HTTP状态
  • /
    def Redirect(url:String,status:Int):Result = Redirect(url,Map.empty,status)

所以你的 Redirect 应该是:

Redirect("/books")

Update

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

id 需要是 number 类型的输入而不是 text ,以及 price .

2 years ago

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

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