Learn Java for Web Development

(Tina Meador) #1

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



  1. <?xml version="1.0" encoding="UTF-8"?>

  2. <beans:beans xmlns="http://www.springframework.org/schema/mvc"

  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans=
    "http://www.springframework.org/schema/beans"

  4. xmlns:context="http://www.springframework.org/schema/context"

  5. xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/
    schema/mvc/spring-mvc.xsd

  6. http://www.springframework.org/schema/beans http://www.springframework.org/schema/
    beans/spring-beans.xsd

  7. http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">




Documentation