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

}

}

相关推荐
青云交9 分钟前
Java 大视界 -- Java 大数据在智慧交通停车场智能管理与车位预测中的应用实践
java·数据采集·数据清洗·智慧交通·停车场智能管理·智能收费系统·车位预测
豐儀麟阁贵15 分钟前
4.4数组的基本操作
java·开发语言·数据结构·算法
组合缺一20 分钟前
全球首个支持 IETF JSONPath (RFC 9535) 标准的 Java 框架,Snack4-Jsonpath v4.0.0 发布
java·开发语言·json·jsonpath
智海观潮28 分钟前
JVM垃圾回收器、内存分配与回收策略
java·大数据·jvm
vx Biye_Design33 分钟前
servlet宠物医院管理系统-计算机毕业设计源码77418
java·vue.js·spring·servlet·eclipse·mybatis
程序员小凯33 分钟前
Spring Boot API文档与自动化测试详解
java·spring boot·后端
照物华36 分钟前
构建优雅的 Spring Boot Starter:Bean 注册与属性绑定的两大机制
java·spring boot
pcm12356737 分钟前
内置线程池的核心参数分析配置
java·开发语言
考虑考虑42 分钟前
解决idea导入项目出现不了maven
java·后端·maven
代码不停1 小时前
JavaEE初级 多线程案例(单例模式、阻塞队列、线程池、定时器)
java·开发语言·单例模式·java-ee