Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Service
- public Account save(Account account) throws Exception {
- if (StringUtils.isEmpty(account.getServiceName())) {
- throw new Exception("Service name is required");
- }
- if (StringUtils.isEmpty(account.getEmail())) {
- throw new Exception("Email is required");
- }
- if (account.getId() != null && existsById(account.getId())) {
- throw new Exception("Account with id: " + account.getId() + " already exists");
- }
- return accountRepository.save(account);
- }
- /Controller
- @PostMapping(value = "/accounts/add")
- public String addAccount(Model model, @ModelAttribute("account") Account account) {
- try {
- Account newAccount = accountService.save(account);
- return "redirect:/accounts/" + newAccount.getId();
- } catch (Exception e) {
- String errorMessage = e.getMessage();
- logger.error(errorMessage);
- model.addAttribute("errorMessage", errorMessage);
- model.addAttribute("add", true);
- return "account-edit";
- }
- }
- //account-edit
- <form action="${urlAction}" name="account" method="post">
- <table border="0">
- <#if account.id??>
- <tr>
- <td>ID</td>
- <td>:</td>
- <td>${account.id}</td>
- </tr>
- </#if>
- <tr>
- <td>Service</td>
- <td>:</td>
- <td><label>
- <input type="text" name="service" value="${(account.serviceName)!''}"/>
- </label></td>
- </tr>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement