Postprocessing the form

Let's process the form using the following steps:

  1. Let's add a method in AddController, which will be invoked on form submission for the URL addBook.htm, as follows:
@RequestMapping("/addBook.htm") 
public ModelAndView addBook(@ModelAttribute("book") Book book) 
 throws Exception { 
  ModelAndView modelAndView = new ModelAndView(); 
  modelAndView.setViewName("display"); 
  //later on the list will be fetched from the table 
  List<Book>books=new ArrayList(); 
  books.add(book); 
  modelAndView.addObject("book_list",books); 
  return modelAndView; 
} 

When the user submits the form, the values entered by him will get bound to the bean data members, giving an instance of the Book bean. Annotating the book argument by @ModelAttribute facilitates developers to use the bean instance that has values bound to it. Now there is no need to read the individual parameters and further things to get and set an instance of Book.

As we already have the display.jsp page to display the books, we are just reusing it here. The book details entered by the user can later be added to the book table.

  1. Run the application, and click on the link to get the form. Fill in the form, and submit it to get the following output:
  1. The output list shows the book details, but without the price as in the output. The price has no value, simply because we haven't set any. We want the price list with some predefined values. Let's move on to discuss preprocessing of the form.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.238.70