使用Spring的ModelAttribute时,Form会出现400 Bad Request错误

时间:2016-07-19 10:30:59

标签: spring-mvc

我有html表格,我转发给春天控制器。如果我使用getParameter但是使用modelAttribute它会说400错误请求错误,它的工作正常。

  

这是我的控制器代码

@Controller
public class BookController {



    @RequestMapping (value="/addBook")
    public String addBook(@ModelAttribute Book book){

        System.out.println(book.getBookName());
        bookService.addBooks(book);


        return "index";
    }


}
  

这是Book model Code

@Entity
@Table (name = "Book")
public class Book {

        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Column(name="ID",columnDefinition = "BIGINT NOT NULL AUTO_INCREMENT")
        private long bookId;
        @Column(name="book_code",columnDefinition = "VARCHAR(200) NOT NULL")
        private String bookCode;
        private String bookName;
        private String author;
        @Temporal (TemporalType.DATE)
        private Date dateOfArrival;
        private Double price;
        private String rackNo;
        private int numberOfBook;
        private String subjectCode;

        public Book() {
            super();
        }

        public Book(String bookCode, String bookName, String author,
                Date dateOfArrival, Double price, String rackNo,
                int numberOfBook, String subjectCode) {
            super();
            this.bookCode = bookCode;
            this.bookName = bookName;
            this.author = author;
            this.dateOfArrival = dateOfArrival;
            this.price = price;
            this.rackNo = rackNo;
            this.numberOfBook = numberOfBook;
            this.subjectCode = subjectCode;
        }



        public String getBookCode() {
            return bookCode;
        }



        public long getBookId() {
            return bookId;
        }

        public void setBookId(long bookId) {
            this.bookId = bookId;
        }

        public void setBookCode(String bookCode) {
            this.bookCode = bookCode;
        }

        public String getBookName() {
            return bookName;
        }

        public void setBookName(String bookName) {
            this.bookName = bookName;
        }

        public String getAuthor() {
            return author;
        }

        public void setAuthor(String author) {
            this.author = author;
        }

        public Date getDateOfArrival() {
            return dateOfArrival;
        }

        public void setDateOfArrival(Date dateOfArrival) {
            this.dateOfArrival = dateOfArrival;
        }

        public Double getPrice() {
            return price;
        }

        public void setPrice(Double price) {
            this.price = price;
        }

        public String getRackNo() {
            return rackNo;
        }

        public void setRackNo(String rackNo) {
            this.rackNo = rackNo;
        }

        public int getNumberOfBook() {
            return numberOfBook;
        }

        public void setNumberOfBook(int numberOfBook) {
            this.numberOfBook = numberOfBook;
        }

        public String getSubjectCode() {
            return subjectCode;
        }

        public void setSubjectCode(String subjectCode) {
            this.subjectCode = subjectCode;
        }

}
  

我怀疑问题是由于使用日期   请帮帮我

1 个答案:

答案 0 :(得分:0)

我会尝试两件事。

  1. 在您的模型中添加:

    //here use the same pattern of date your send from the view
    @DateTimeFormat(pattern = "dd/MM/yyyy")
    private Date dateOfArrival;
    
  2. 在你的控制器中

    @RequestMapping (value="/addBook")
    public String addBook(@ModelAttribute Book book, BindingResult result){
    ...
    
  3. 然后,您将能够调试方法并了解问题。