2016-07-19 5 views
0

Ich habe HTML-Formular, das ich an Feder-Controller weiterleiten. Es funktioniert gut, wenn ich getParameter benutze, aber mit modelAttribute heißt es 400 fehlerhafte Anfrage Fehler.Formular gibt 400 Bad Request Fehler bei der Verwendung mit Spring ModelAttribute

Hier ist mein Controller-Code

@Controller 
public class BookController { 



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

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


     return "index"; 
    } 


} 

Dies ist Buch-Modell-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; 
     } 

} 

Ich habe Zweifel, dass das Problem mit Datum fällig ist Bitte hilf mir

+0

Ich würde diesen Parameter, BindingResult-Ergebnis, zu Ihrer Controller-Methode hinzufügen und die Methode debuggen, um den Grund des Fehlers genau zu sehen. Wenn das Datum Ihr Problem ist, müssen Sie wahrscheinlich das Datum binden – cralfaro

+0

Haben Sie die Antwort überprüft? – cralfaro

Antwort

0

Ich würde 2 Dinge versuchen.

  1. in Ihrem Modell fügen Sie diese:

    //here use the same pattern of date your send from the view 
    @DateTimeFormat(pattern = "dd/MM/yyyy") 
    private Date dateOfArrival; 
    
  2. In Ihrem Controller

    @RequestMapping (value="/addBook") 
    public String addBook(@ModelAttribute Book book, BindingResult result){ 
    ... 
    

Dann können Sie Ihre Methode zum Debuggen und das Problem kennen.

Verwandte Themen