Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.61 KB | None | 0 0
  1. //Service
  2. public Account save(Account account) throws Exception {
  3.         if (StringUtils.isEmpty(account.getServiceName())) {
  4.             throw new Exception("Service name is required");
  5.         }
  6.         if (StringUtils.isEmpty(account.getEmail())) {
  7.             throw new Exception("Email is required");
  8.         }
  9.  
  10.         if (account.getId() != null && existsById(account.getId())) {
  11.             throw new Exception("Account with id: " + account.getId() + " already exists");
  12.         }
  13.  
  14.         return accountRepository.save(account);
  15.     }
  16.  
  17.  
  18. /Controller
  19. @PostMapping(value = "/accounts/add")
  20.     public String addAccount(Model model, @ModelAttribute("account") Account account) {
  21.         try {
  22.             Account newAccount = accountService.save(account);
  23.             return "redirect:/accounts/" + newAccount.getId();
  24.         } catch (Exception e) {
  25.             String errorMessage = e.getMessage();
  26.             logger.error(errorMessage);
  27.             model.addAttribute("errorMessage", errorMessage);
  28.             model.addAttribute("add", true);
  29.             return "account-edit";
  30.         }
  31.     }
  32.  
  33. //account-edit
  34. <form action="${urlAction}" name="account" method="post">
  35.     <table border="0">
  36.         <#if account.id??>
  37.             <tr>
  38.                 <td>ID</td>
  39.                 <td>:</td>
  40.                 <td>${account.id}</td>
  41.             </tr>
  42.         </#if>
  43.         <tr>
  44.             <td>Service</td>
  45.             <td>:</td>
  46.             <td><label>
  47.                     <input type="text" name="service" value="${(account.serviceName)!''}"/>
  48.                 </label></td>
  49.         </tr>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement