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

}

}

相关推荐
Predestination王瀞潞2 分钟前
JDK安装及环境变量配置
java·linux·开发语言
谷哥的小弟22 分钟前
Spring Framework源码解析——PropertiesLoaderUtils
java·后端·spring·框架·源码
JIngJaneIL32 分钟前
基于java+ vue助农电商系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot·后端
q_191328469537 分钟前
基于Springboot+MySQL+RuoYi的会议室预约管理系统
java·vue.js·spring boot·后端·mysql·若依·计算机毕业设计
元气满满-樱1 小时前
Tomcat理论
java·tomcat
一只叫煤球的猫1 小时前
从夯到拉,锐评13个Java Web框架
java·后端·程序员
heartbeat..1 小时前
JUC 在实际业务场景的落地实践
java·开发语言·网络·集合·并发
tryxr1 小时前
线程安全的类 ≠ 线程安全的程序
java·开发语言·vector·线程安全
rchmin1 小时前
Java内存模型(JMM)详解
java·开发语言
Wpa.wk1 小时前
Tomcat的安装与部署使用 - 说明版
java·开发语言·经验分享·笔记·tomcat