【问题标题】:Form Gives 400 Bad Request error when using with Spring's ModelAttribute与 Spring 的 ModelAttribute 一起使用时,表单会出现 400 Bad Request 错误
【发布时间】:2016-07-19 10:30:58
【问题描述】:

我有 html 表单,我将其转发给 spring 控制器。如果我使用 getParameter 但使用 modelAttribute 它会显示 400 bad request error。

这是我的控制器代码

@Controller
public class BookController {



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

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


        return "index";
    }


}

这是图书模型代码

@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;
        }

}

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

【问题讨论】:

  • 我会将此参数 BindingResult 结果添加到您的控制器方法中并调试该方法以查看错误的确切原因。如果日期是您的问题,您可能需要绑定日期
  • 您查看回复了吗?

标签: spring-mvc


【解决方案1】:

我会尝试两件事。

  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){
    ...
    

然后你就可以调试你的方法并知道问题所在。

【讨论】:

    猜你喜欢
    • 2011-05-31
    • 1970-01-01
    • 2015-11-16
    • 1970-01-01
    • 2013-08-21
    • 1970-01-01
    • 1970-01-01
    • 2014-03-20
    • 2023-01-03
    相关资源
    最近更新 更多