PostAccounts.java 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. package bigbank.web;
  2. import javax.servlet.http.HttpServletRequest;
  3. import javax.servlet.http.HttpServletResponse;
  4. import org.springframework.util.Assert;
  5. import org.springframework.web.bind.ServletRequestUtils;
  6. import org.springframework.web.servlet.ModelAndView;
  7. import org.springframework.web.servlet.mvc.Controller;
  8. import bigbank.Account;
  9. import bigbank.BankService;
  10. public class PostAccounts implements Controller {
  11. private BankService bankService;
  12. public PostAccounts(BankService bankService) {
  13. Assert.notNull(bankService);
  14. this.bankService = bankService;
  15. }
  16. public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
  17. // Security check (this is unnecessary if Spring Security is performing the authorization)
  18. // if (request.isUserInRole("ROLE_TELLER")) {
  19. // response.sendError(HttpServletResponse.SC_FORBIDDEN, "You must be a teller to post transactions");
  20. // return null;
  21. // }
  22. // Actual business logic
  23. Long id = ServletRequestUtils.getRequiredLongParameter(request, "id");
  24. Double amount = ServletRequestUtils.getRequiredDoubleParameter(request, "amount");
  25. Account a = bankService.readAccount(id);
  26. bankService.post(a, amount);
  27. return new ModelAndView("redirect:listAccounts.html");
  28. }
  29. }