@Override
protected void initBinder(HttpServletRequest request,
ServletRequestDataBinder binder) throws Exception {
binder.registerCustomEditor(Date.class,
new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), false));
}
@Override
protected ModelAndView onSubmit(Object command) throws Exception {
Person person = (Person) command;
personDao.persist(person);
Map<String, String> model = new HashMap<String, String>();
model.put("suggestedBook",
suggestBook(person.getFavoriteProgrammingLanguage()));
model.put(getCommandName(), person);
return new ModelAndView(getSuccessView(), model);
}
private String suggestBook(String favoriteProgrammingLanguage) {
Language language = Language.create(favoriteProgrammingLanguage);
return language.recommendBookTitle();
}
}
As you may have noticed, the controller now delegates persistence to a PersonDaoclass.
Thus, a setter method is provided, setPersonDao(), so that the ApplicationContextcan easily
inject an instance.
The old doSubmitAction()is now replaced with the more flexible onSubmit() method,
allowing us to return a ModelAndViewobject. Notice how we simply call getSuccessView()to
return the configured success view. We are also now required to manually add the person
object into the model, which was previously automatically added.
■Tip If you return nullfrom onSubmit(),a default ModelAndViewwill be created with getSuccessView()
and errors.getModel().
The business logic of choosing a recommended book title is delegated to a Language
object. This follows the recommendations of delegating any business logic to the service layer
or other POJOs in the system.
CHAPTER 6 ■THE CONTROLLER MENAGERIE 163