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

}

}

相关推荐
kingx33 分钟前
Bugku 3122 谷歌小恐龙题解:纯离线逆向 Vault 的 AES-GCM 加密资源(附 Python 解密器)
java·逆向·ctf·bugku·aes-gcm
DevOpenClub2 小时前
职教高考控制线 API:招生类别枚举、筛选与空分数含义
java·数据库·高考
露天赏雪3 小时前
第3篇:Sentinel熔断降级实战:大模型接口抖动时的服务兜底方案
java·开发语言·人工智能·spring boot·spring·servlet·sentinel
天天被压力3 小时前
【A股分钟级高频数据完全手册 #06】北交所分钟级数据接口:京市股票Pro版最新分时
java·人工智能·python
天天被压力3 小时前
【A股分钟级高频数据完全手册 #05】指数历史分钟数据接口:Pro版沪深指数分时回放
java·人工智能·python
captain3764 小时前
▲统一功能——DispatcherServlet源码分析
java·spring·java-ee·log4j·maven
金銀銅鐵4 小时前
JDK 21 中 List.of 的实现细节:从 List12 到 ListN
java·后端·ai编程
yeccheng5 小时前
MySQL 索引超详解:从底层 B+ 树到面试高频考点
java·开发语言·jvm·mysql·面试
这个昵称不容易6 小时前
SpringBoot 读书笔记共享平台部署实战:97 个 Java 文件 / 113 个接口 / 10 张表
java·springboot·毕设
金銀銅鐵8 小时前
一个 Java 文件实现《孫子兵法·始計篇》中英对照阅读器
java·后端·ai编程