表单的“.fill()”函数未给出任何结果或错误

'.fill()' function for form not giving any result or error

我希望我的表单具有初始值。所以我使用了 .fill 函数。但它仍然给我一个空表格。我的代码有什么问题?

我的 BooksController 有:

    public Result edit(Integer id){
        Book book = Book.findById(id);
        if(book==null){
            return notFound("book not found");
        }
        Form<Book> bookForm = formFactory.form(Book.class).fill(book);
        return ok(edit.render(bookForm));
    }

    public Result update(Http.Request request){
        Book book = formFactory.form(Book.class).bindFromRequest(request).get();
        Book oldBook = Book.findById(book.id);
        if(oldBook==null){return notFound("book not found");}

        oldBook.title=book.title;
        oldBook.author=book.author;
        oldBook.price=book.price;
        return redirect(routes.BooksController.front());
    }

我的 edit.scala.html 视图有:

@(bookForm : Form[Book])
@import helper._


<html>
    <head>
        <title>edit Book</title>
    </head>
    <body>
        <h3>edit book</h3>
        @helper.form(routes.BooksController.update){
            @helper.inputText(bookForm("id"))
            @helper.inputText(bookForm("title"))
            @helper.inputText(bookForm("author"))
            @helper.inputText(bookForm("price"))

            <button type="submit">edit Book</button>
        }
    </body>
</html>

我的路线文件有:

+nocsrf
GET     /books/edit/:id             controllers.BooksController.edit(id: Integer)
+nocsrf
POST    /books/edit                 controllers.BooksController.update(request : Request)

据我了解这段代码

oldBook.title=book.title;
oldBook.author=book.author;
oldBook.price=book.price;

您没有 getter 和 setter,因此您需要激活 "direct access" 字段。

您可以在 conf/application.conf 文件中完成:

play.forms.binding.directFieldAccess = true

您也可以通过调用 .withDirectFieldAccess(true):

仅为一种形式启用 "direct access"
Form<Book> bookForm = formFactory.form(Book.class).withDirectFieldAccess(true).fill(book);

更多信息:https://www.playframework.com/documentation/2.7.x/JavaForms#Defining-a-form