简体   繁体   English

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

[英]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.

相关问题 单击提交按钮时 controller 中的调用方法? - call method in controller when submit button clicked? 单击提交按钮时不执行任何操作 - Submit button does nothing when clicked 我正在从数据库中填充我的下拉列表,但是选择后,当单击提交按钮时,在我的Java代码中存在的方法中我得到了空值 - I am populating my dropdown from DB but after selecting I am getting null value in my method present in my java code when submit button is clicked 单击“提交”按钮时如何设置半透明的jframe? - how to set semi-transparent jframe when “submit” button is clicked? 单击提交按钮时无操作,但应注册用户 - No action when submit button is clicked, but it should register a user JAVA GUI - 单击时我的提交按钮没有响应 - JAVA GUI - My submit button does not respond when clicked 从表单读取时如何忽略空值? - How to ignore null values when reading from a form? 从Mainactivity中单击按钮时,列表视图中的所有复选框都无法从不可见状态显示 - All checkbox in listview is not getting visible from invisible state when button is clicked from Mainactivity 如何防止我的android中的“提交”按钮在多次单击时给我多个分数? - How do I prevent my submit button in android from giving me multiple scores when clicked multiple times? 单击一个循环时创建的按钮都在做相同的事情 - Buttons created in a loop are all doing the same thing when clicked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM