@Controller
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/")
public String index(Model model) {
List<User> users = userService.findAll();
model.addAttribute("users", users);
return "index";
}
@GetMapping("/user/{id}")
public String getUser(@PathVariable Long id, Model model) {
User user = userService.findById(id);
model.addAttribute("user", user);
return "user";
}
@GetMapping("/user/create")
public String createUserForm(Model model) {
model.addAttribute("user", new User());
return "create_user";
}
@PostMapping("/user/create")
public String createUser(@ModelAttribute User user) {
userService.save(user);
return "redirect:/";
}
@GetMapping("/user/edit/{id}")
public String editUserForm(@PathVariable Long id, Model model) {
User user = userService.findById(id);
model.addAttribute("user", user);
return "edit_user";
}
@PostMapping("/user/edit/{id}")
public String editUser(@PathVariable Long id, @ModelAttribute User user) {
user.setId(id);
userService.update(user);
return "redirect:/";
}
@GetMapping("/user/delete/{id}")
public String deleteUser(@PathVariable Long id) {
userService.deleteById(id);
return "redirect:/";
}
}