<!DOCTYPE html> <html lang="en" xmlns:th="http://www.w3.org/1999/xhtml"> <head> <meta…
- User
private final UserService userService; @Autowired public UserController(UserService userService) { this.userService = userService; } @GetMapping("/users") public String findAll(Model model){ List<User> users = userService.findAll(); model.addAttribute("users", users); return "ListUser"; } @GetMapping("/ListCreateUser") public String createUserForm(User user){ return "ListCreateUser"; } @PostMapping("/ListCreateUser") public String createUser(User user){ userService.saveUser(user); return "redirect:/users"; } @GetMapping("user-delete/{id}") public String deleteUser(@PathVariable("id") Long id){ userService.deleteById(id); return "redirect:/users"; } @GetMapping("/ListUpdateUser/{id}") public String updateUserForm(@PathVariable("id") Long id, Model model){ User user = userService.findById(id); model.addAttribute("user", user); return "ListUpdateUser"; } @PostMapping("/ListUpdateUser") public String updateUser(User user){ userService.saveUser(user); return "redirect:/users"; }} Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback.
Sat Jun 24 18:46:36 MSK 2023 There was an unexpected error (type=Not Found, status=404).
- Assistant
The requested page or resource was not found on the server. It is possible that the URL or endpoint does not exist or has been removed. Please check the URL and try again.
- User
Fix this code. So that I can add new users.
- Assistant
To fix the code, make sure that the Thymeleaf form is properly connected to the UserController's createUser method. Here are the steps:
- In the UserController, create a new method named "createUser" that accepts a User object as a parameter and uses the userService to save the user to the database.
@PostMapping("/ListCreateUser") public String createUser(@ModelAttribute("user") User user){ userService.saveUser(user); return "redirect:/users"; }- Add a ModelAttribute to the createUserForm method to bind the empty User object to the form.
@GetMapping("/ListCreateUser") public String createUserForm(@ModelAttribute("user") User user){ return "ListCreateUser"; }- Update the form action in the Thymeleaf template to point to the /ListCreateUser endpoint.
- User
Fix this code. So that I can add new users.
private final UserService userService; @Autowired public UserController(UserService userService) { this.userService = userService; } @GetMapping("/users") public String findAll(Model model){ List<User> users = userService.findAll(); model.addAttribute("users", users); return "ListUser"; } @GetMapping("/ListCreateUser") public String createUserForm(@ModelAttribute("user") User user){ return "ListCreateUser"; } @PostMapping("/ListCreateUser") public String createUser(@ModelAttribute("user") User user){ userService.saveUser(user); return "redirect:/users"; } @GetMapping("user-delete/{id}") public String deleteUser(@PathVariable("id") Long id){ userService.deleteById(id); return "redirect:/users"; } @GetMapping("/ListUpdateUser/{id}") public String updateUserForm(@PathVariable("id") Long id, Model model){ User user = userService.findById(id); model.addAttribute("user", user); return "ListUpdateUser"; } @PostMapping("/ListUpdateUser") public String updateUser(User user){ userService.saveUser(user); return "redirect:/users"; } - Assistant
The changes included are as follows:
HTML CODE:
- Change double quotes marks to normal quotes (" to “ ”)
- Add curly braces to field attribute in firstName and lastName's input tags