Expert Spring MVC and Web Flow

(Dana P.) #1

The real action happens inside onSubmit(), the life cycle callback for handling the com-
mand bean when the form is submitted. If this method is called, it is assumed that the bean
was successfully created, populated with values from the form, and validated correctly. By
now you can assume that the command bean is ready to be processed.
The onSubmit()method should delegate to the service layer, as we are doing here with the
call to findFlights(). It also is responsible for generating the ModelAndViewobject, to be used
when rendering the success view. As you can see, we are including the search results and the
original command bean into the model, so that we can display the matching flights and the orig-
inal search criteria.
With the matching flights located and included in the model, the success view will be
rendered.


SearchFlightsController Configuration


Like we did for HomeController, the SearchFlightsControllerwill be defined inside the
spring-servlet.xmland thus the WebApplicationContext. We will also map this controller to
the URI /search, which will be used for both viewing the search form and handling the form
submission. Refer to Listing 4-17.


Listing 4-17.spring-servlet.xml Additions for SearchFlightsController


<bean name="/search"
class="com.apress.expertspringmvc.flight.web.SearchFlightsController">




No other configuration is required, for the environment was previously configured for the
first use case.


Summary


The SearchFlightsControlleris a basic implementation of the SimpleFormController. It
leaves nearly all of the work flow up to the superclass, implementing only the onSubmit()
method to process the command bean. The processing is simply delegated to the service layer,
creating a clean separation of concerns.
As seen in the constructor, a SimpleFormControllerrequires two Views, one for the initial
form view, containing the XHTML form, and one for the success view, rendered after a suc-
cessful form submission. Let’s look at both of these JSP pages now.


Form View


The first XHTML page we will create contains the search form, shown in Listing 4-18. Again,
for simplicity’s sake, we will use JSP as the template language, but you can use any of Spring’s
supported template systems that best suits your needs.
Note that, for the time being, we are ignoring validation issues such as displaying valida-
tion errors. All of the work we do here is completely compatible with validation, but for the
sake of showing you the most functionality in this chapter without spilling over to hundreds
of pages, we are glossing over validation. We’re big fans of validated data, but there’s a whole
chapter that covers it nicely.


CHAPTER 4 ■JUMP INTO SPRING MVC 69
Free download pdf