Expert Spring MVC and Web Flow

(Dana P.) #1
The constructor is used to declaratively configure the class, defining the command bean,
the command name, and the view names for the form work flow.


  • setCommandName()defines the name of the command bean, when referenced by the
    view page. You will see this name referenced with the tag in the
    JSP in Listing 4-8, defining the bean to be used for the entire XHTML form.

  • setCommandClass()defines the class to be used for the command bean. This can be any
    POJO with getters and setters, and it can include both simple and complex types for
    properties.

  • setFormView()defines the logical name for the view used when initially viewing the
    form. This view will also be displayed if validation fails, so that the user can correct any
    mistakes. Remember that this view name is resolved to an actual Viewinstance via the
    ViewResolver.

  • setSuccessView()defines the logical name for the view to display when form submis-
    sion finished correctly. This view will receive the objects from the model when form
    processing is complete.


■NoteYou may choose to define these properties in the bean definition XML file when you declare the
Controller.However, because these configurations are fairly static, we recommend the constructor as a
better place to set the properties. Anything to keep the amount of XML to a minimum is usually helpful.

Notice that what you don’t see in the code is any special handling to display the form
itself. The SimpleFormControllerhandles the initial HTTP GET request and displays the initial
form view. Most of the time, you will concern yourself only with handling the form submis-
sion.
Just like with the HomeController, this Controllerdelegates the real work of the form sub-
mission to the service layer. We see that this Controllerincludes the setFlightService()
method so that the ApplicationContextcan inject this dependency.
The initBinder()method is a life cycle callback method provided so that you
may register any custom PropertyEditors required for your command bean. Because the
SearchFlightsbean has properties of type java.util.Date, we need to create an instance
of CustomDateEditorwith the allowed date format. The registerCustomEditor()method
essentially says, “Whenever you see a property of type Date.class, use this CustomDateEditor
to convert the Stringfrom the request parameter into an instance of Date.class.”

■NoteSpring, out of the box, is configured with many different PropertyEditors to support many of the
basic types, such as ints,booleans, and arrays of Strings. These PropertyEditors you do not need to
register. You are required to register an editor only if it requires specific information in order to function,
as is the case here with a custom date format.

68 CHAPTER 4 ■JUMP INTO SPRING MVC

Free download pdf