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

}

}

相关推荐
吴声子夜歌8 小时前
ApacheCommons——commons-text(模板替换与文本算法)
java·开发语言·算法·apache
Haooog8 小时前
Agent 开发中的 Memory:State、短期记忆、长期记忆与 Memory Retrieval
java·agent·memory
砚底藏山河8 小时前
【量化纯GET实战 #23】多股票相关性:用收益率看板块联动
java·数据库·python·金融·数据分析
抠脚小弟8 小时前
Spring Cache 使用详解:从入门到实战
java·spring
ZGG0039 小时前
MySQL 索引详解:B+ 树、聚簇索引与最左前缀
java·数据库·mysql
萧瑟余晖9 小时前
Java深入解析篇五十四之对象模型详解
java·开发语言
张文是假的啊9 小时前
Java | record | Controller逻辑
java·开发语言
勿忘,瞬间9 小时前
Mybatis高阶
java·数据库·mybatis
嘻哈baby10 小时前
Go 函数中的参数为什么不支持默认值?
java·开发语言·jvm
慧都小项11 小时前
MyEclipse 2026:当 Agent、MCP 与 Java 26 进入 Eclipse 工作流
java·eclipse·springboot·copilot·myeclipse