[英]All values null when submit button clicked from a created form
I'm trying to get the value from form and when I debug it, it shows me only null. 我正在尝试从表单获取值,当我调试它时,它只显示null。 Is there any thing wrong with my binding request?
我的绑定请求有什么问题吗? Or something else?
或者是其他东西?
create.scala.index: 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: 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: Book.java:
public static Set<Book> allBook(){
return books;
}
public static void add(Book book){
books.add(book);
}
This is my whole book.java class 这是我的整本书。java类
package models;
import java.util.HashSet;
import java.util.Set;
public class Book {
public Integer id;
public String title;
public Integer price;
public String author;
public Book() {
}
public Book(Integer id, String title, Integer price, String author) {
this.id = id;
this.title = title;
this.price = price;
this.author = author;
}
private static Set<Book> books;
static {
books= new HashSet<>();
books.add(new Book(1,"C++",1100,"Abc"));
books.add(new Book(2,"JAVA",1100,"Scd"));
}
public static Set<Book> allBook(){
return books;
}
public static Book findById(Integer id){
for(Book books:books) {
if (id.equals(books.id)){
return books;
}
}
return null;
}
public static void add(Book book){
books.add(book);
}
public static boolean remove(Book book){
return books.remove(book);
}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.