从创建的表单单击提交按钮时,所有值都为null

时间:2017-12-20 10:02:47

标签: java scala playframework

我正在尝试从表单中获取值,当我调试它时,它只显示null。我的绑定请求有什么问题吗?或其他什么?

create.scala.index:

@(bookform : Form[Book])
@import helper._
<html>
<head>
    <title>Create Book</title>
</head>
<body>
    <h1>All Books</h1>

    @helper.form(CSRF(routes.BooksController.save())){
        @helper.inputText(bookform("id"))
        @helper.inputText(bookform("title"))
        @helper.inputText(bookform("price"))
        @helper.inputText(bookform("author"))

        <input type="submit" value="create book">
    }

BookController.java:

import views.html.books.*;
import javax.inject.Inject;
public class BooksController extends Controller {

    //help to create forms
    @Inject
    FormFactory formFactory;

    //for all book
    public Result index(){
        Set<Book> books= Book.allBook();
        return  ok(index.render(books));
    }

    //create book
    public Result create(){
        Form<Book> bookForm = formFactory.form(Book.class);
        return  ok(create.render(bookForm));
    }

    //to save book
    public Result save(){
        Form<Book> bookForm=formFactory.form(Book.class).bindFromRequest();
        Book book1= bookForm.get();
        Book.add(book1);

        return  redirect(routes.BooksController.index());
    } 
}

Book.java:

public static Set<Book> allBook(){
    return books;
}

public static void add(Book book){
    books.add(book);
}

1 个答案:

答案 0 :(得分:0)

这是我的整本书.java类

slice

}