@RequestMapping public String index(Model model, Principal principal) { model.addAttribute("title", "List User"); model.addAttribute("users", service.getAll()); model.addAttribute("deleteUser", new User()); // System.out.println(gson.toJson(principal)); // System.out.println(gson.toJson(model)); // logger.debug("Model Attribute User"); return "user/user"; }
@RequestMapping(value = "/edit/{userId}", method = RequestMethod.GET) public String editUser(@PathVariable("userId") Long userId, Model model, User user) { user = service.findById(userId); if (user == null) { model.addAttribute("title", "User not found"); model.addAttribute("message", "User with id " + userId + " not found!"); return "errors/404"; } model.addAttribute("title", "Edit User"); model.addAttribute("editUser", user); return "user/edit"; }
@RequestMapping(value = "/delete", method = RequestMethod.POST) public String deleteUser( @ModelAttribute("deleteUser") User deleteUser, BindingResult result, final RedirectAttributes redirectAttr) { if (!result.hasErrors()) { service.deleteById(deleteUser.getId()); redirectAttr.addFlashAttribute("success", true); redirectAttr.addFlashAttribute("message", "Success delete user!"); } else { redirectAttr.addFlashAttribute("error", true); redirectAttr.addFlashAttribute("message", "Can't delete user. Something went wrong."); } return "redirect:/user"; }
@RequestMapping(value = "/update", method = RequestMethod.POST) public String updateUser( @ModelAttribute("editUser") User editUser, BindingResult result, final RedirectAttributes redirectAttr) { if (result.hasErrors()) { redirectAttr.addFlashAttribute("error", true); redirectAttr.addFlashAttribute("message", result.getFieldError().getDefaultMessage()); return "redirect:/user/edit/" + Long.toString(editUser.getId()); } service.update(editUser); redirectAttr.addFlashAttribute("success", true); redirectAttr.addFlashAttribute("message", "Success edit user!"); return "redirect:/user"; }
@RequestMapping(value = "/add", method = RequestMethod.POST) public String addUser( @ModelAttribute("user") User user, BindingResult result, final RedirectAttributes redirectAttr) { if (result.hasErrors()) { redirectAttr.addFlashAttribute("error", true); redirectAttr.addFlashAttribute("message", result.getFieldError().getDefaultMessage()); return "redirect:/user/add"; } service.create(user); redirectAttr.addFlashAttribute("success", true); redirectAttr.addFlashAttribute("message", "Success add user!"); return "redirect:/user"; }