Expert Spring MVC and Web Flow

(Dana P.) #1

complex form handling, so it’s a perfect chance to consolidate these three actions into one
controller. We’ll also take advantage of the built-in exception handling and rudimentary vali-
dation provided by the DataBinder, later in this example.
Listing 6-59 contains a full example of a MultiActionController.


Listing 6-59.ViewAccountController Example


public class ViewAccountController extends MultiActionController {


private AccountService accountService;

public ViewAccountController() throws ApplicationContextException {
setSupportedMethods(new String[]{METHOD_GET});
}

public void setAccountService(AccountService accountService) {
this.accountService = accountService;
}

public ModelAndView findByUsername(HttpServletRequest request,
HttpServletResponse response, SearchCriteria criteria) {
Account account = accountService.findAccountByUsername(
criteria.getSearchBy());
return new ModelAndView("viewAccount", "account", account);
}

public ModelAndView findByFirstName(HttpServletRequest request,
HttpServletResponse response, SearchCriteria criteria) {
List<Account> accounts = accountService.findAccountsByFirstName(
criteria.getSearchBy());
return new ModelAndView("viewAccounts", "accounts", accounts);
}

public ModelAndView findByLastName(HttpServletRequest request,
HttpServletResponse response, SearchCriteria criteria) {
List<Account> accounts = accountService.findAccountsByLastName(
criteria.getSearchBy());
return new ModelAndView("viewAccounts", "accounts", accounts);
}

public static class SearchCriteria {
private String searchBy;

public String getSearchBy() {
return searchBy;
}

CHAPTER 6 ■THE CONTROLLER MENAGERIE 173
Free download pdf