260 CHAPTER 5: Building Java Web Applications with Spring Web MVC
Line 29: The BookValidator class is injected using the setter method.
Line 63: In processSubmit(), validate() of the BookValidator is called to check
whether the book details are entered by the user. validate() is passed the value
of the Book model and the BindingResult object to hold errors, if any.
Line 66: A check is made whether the result variable holds any errors. If there
are errors, then the application displays the same page with the error messages.
If there are no errors, that is, the user has entered all the correct data, then the
application displays the list of the book details along with the newly entered
book details.
Configuring the Validator
Now you have to declare the validator for the URL addBook.html in bookstore-servlet.xml, as illustrated
in Listing 5-52.
Listing 5-52. Declaring the BookValidator
- <?xml version="1.0" encoding="UTF-8"?>
- <beans:beans xmlns="http://www.springframework.org/schema/mvc"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans=
"http://www.springframework.org/schema/beans" - xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/
schema/mvc/spring-mvc.xsd - http://www.springframework.org/schema/beans http://www.springframework.org/schema/
beans/spring-beans.xsd - http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">