Expert Spring MVC and Web Flow

(Dana P.) #1

Summary


Command beans are POJOs that encapsulate the data from a form submission. Command
bean classes must have getters and setters whose property names match the names of the
form fields. Spring MVC uses PropertyEditors to help convert Stringvalues from the request
into the expected types of the properties of the command bean class.


SearchFlightsController


With the service layer and the SearchFlightsclass already created, we can quickly build the
SearchFlightsController, shown in Listing 4-16.


Listing 4-16.SearchFlightsController Class


public class SearchFlightsController extends SimpleFormController {


private FlightService flights;

public SearchFlightsController() {
setCommandName("searchFlights");
setCommandClass(SearchFlights.class);
setFormView("beginSearch");
setSuccessView("listFlights");
}

public void setFlightService(FlightService flights) {
this.flights = flights;
}

@Override
protected void initBinder(HttpServletRequest req,
ServletRequestDataBinder binder) throws Exception {
binder.registerCustomEditor(Date.class, new CustomDateEditor(
new SimpleDateFormat("yyyy-MM-dd HH"), true));
}

@Override
protected ModelAndView onSubmit(Object command) throws Exception {
SearchFlights search = (SearchFlights) command;
ModelAndView mav = new ModelAndView(getSuccessView());
mav.addObject("flights", flights.findFlights(search));
mav.addObject("searchFlights", search);
return mav;
}

}


CHAPTER 4 ■JUMP INTO SPRING MVC 67
Free download pdf