PostAccounts.java 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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. // throw new AccessDeniedException("You must be a teller to post transactions (Spring Security message)"); // only for Spring Security managed authentication
  20. // }
  21. // Actual business logic
  22. Long id = ServletRequestUtils.getRequiredLongParameter(request, "id");
  23. Double amount = ServletRequestUtils.getRequiredDoubleParameter(request, "amount");
  24. Account a = bankService.readAccount(id);
  25. bankService.post(a, amount);
  26. return new ModelAndView("redirect:listAccounts.html");
  27. }
  28. }