254 CHAPTER 5: Building Java Web Applications with Spring Web MVC
Line 47: The reference data is put into the model so that the form view can
access it, by annotating the method name with @ModelAttribute.
When @ModelAttribute is placed on a method parameter, it maps a model
attribute to the specific annotated method parameter. This is how the controller
gets a reference to the object holding the data entered in the form.
@ModelAttribute annotation informs the Spring MVC framework that the
authorList instance should be assigned as an instance of the Author class
and should be passed to populate the AuthorList().
Line 53: The form submission is handled by annotating the method name with
@RequestMapping(method= RequestMethod.POST).
Line 54: processSubmit() accepts POST requests; that is, an HTTP POST for
/new_book.html invokes processSubmit(). processSubmit() processes the form
data. processSubmit() takes three parameters:
@ModelAttribute(value="book") Book book: The model attribute annotation
informs the Spring MVC framework that the Book model instance should be
assigned as an instance of the Book class and should be passed to the method
processSubmit().
BindingResult result: Spring determines errors, if any, during the creation
of the Book class. If it finds errors, its description is passed to the method as
a BindingResult instance.
SessionStatus status: SessionStatus is a status handle for marking form
processing as complete.
Line 57: The redirect: prefix in the return statement triggers an HTTP redirect
back to the browser. This is necessary when delegating the response to another
controller, rather than just rendering the view.
Listing 5-47 illustrates the modified service layer of the bookstore application for form processing.
Listing 5-47. BookService
package com.apress.bookstore.service;
import java.util.LinkedList;
import java.util.List;
import com.apress.bookstore.model.Author;
import com.apress.bookstore.model.Book;
public class BookService {
private static List
static {
Author author1 = new Author();
author1.setAuthorId((long) 1);