userController

@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:/";

}

}

相关推荐
B612 little star king13 分钟前
力扣29. 两数相除题解
java·算法·leetcode
野犬寒鸦14 分钟前
力扣hot100:环形链表(快慢指针法)(141)
java·数据结构·算法·leetcode·面试·职场和发展
上官浩仁20 分钟前
springboot synchronized 本地锁入门与实战
java·spring boot·spring
Gogo81621 分钟前
java与node.js对比
java·node.js
SmartJavaAI27 分钟前
Java调用Whisper和Vosk语音识别(ASR)模型,实现高效实时语音识别(附源码)
java·人工智能·whisper·语音识别
用户37215742613531 分钟前
Python 高效实现 Word 转 PDF:告别 Office 依赖
java
渣哥36 分钟前
Java ThreadPoolExecutor 动态调整核心线程数:方法与注意事项
java
Miraitowa_cheems1 小时前
LeetCode算法日记 - Day 38: 二叉树的锯齿形层序遍历、二叉树最大宽度
java·linux·运维·算法·leetcode·链表·职场和发展
稻草猫.1 小时前
Java多线程(一)
java·后端·java-ee·idea
躲在云朵里`1 小时前
Spring Scheduler定时任务实战:从零掌握任务调度
java·数据库·mybatis