|
@@ -0,0 +1,67 @@
|
|
|
+package com.sf.controller;
|
|
|
+
|
|
|
+import com.sf.entity.Role;
|
|
|
+import com.sf.service.RoleService;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Controller;
|
|
|
+import org.springframework.ui.Model;
|
|
|
+import org.springframework.web.bind.annotation.GetMapping;
|
|
|
+import org.springframework.web.bind.annotation.PostMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestParam;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+// 在正确的包路径下 创建正确的名字
|
|
|
+// 创建类之后 要思考 需要处理的是页面还是数据
|
|
|
+@Controller
|
|
|
+@RequestMapping("/role")
|
|
|
+public class RoleController {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private RoleService roleService;
|
|
|
+
|
|
|
+ // 方法 需要返回string 是页面的名字
|
|
|
+ @GetMapping("/list")
|
|
|
+ public String list(Model model) {
|
|
|
+ List<Role> roleList = roleService.queryRoles();
|
|
|
+ model.addAttribute("roleList", roleList);
|
|
|
+ return "demo/list";
|
|
|
+ }
|
|
|
+
|
|
|
+ @GetMapping("/delete")
|
|
|
+ public String delete(@RequestParam("id") int id) {
|
|
|
+ // 常见的删除处理 是删除数据后 重新请求列表页
|
|
|
+ roleService.delete(id);
|
|
|
+ return "redirect:/role/list";
|
|
|
+ }
|
|
|
+
|
|
|
+ @GetMapping("/toAdd")
|
|
|
+ public String toAdd() {
|
|
|
+ System.out.println("toAdd");
|
|
|
+ return "demo/add";
|
|
|
+ }
|
|
|
+
|
|
|
+ @PostMapping("/add")
|
|
|
+ public String add(Role role) {
|
|
|
+ System.out.println("add param: " + role);
|
|
|
+ roleService.add(role);
|
|
|
+ return "redirect:/role/list";
|
|
|
+ }
|
|
|
+
|
|
|
+ @GetMapping("/toUpdate")
|
|
|
+ public String toUpdate(@RequestParam("id") int id, Model model) {
|
|
|
+ System.out.println("toUpdate id: " + id);
|
|
|
+ Role role = roleService.queryRoleById(id);
|
|
|
+ model.addAttribute("role", role);
|
|
|
+ return "demo/update";
|
|
|
+ }
|
|
|
+
|
|
|
+ @PostMapping("/update")
|
|
|
+ public String update(Role role) {
|
|
|
+ System.out.println("update param: " + role);
|
|
|
+ roleService.update(role);
|
|
|
+ return "redirect:/role/list";
|
|
|
+ }
|
|
|
+
|
|
|
+}
|