|
@@ -0,0 +1,73 @@
|
|
|
|
+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.*;
|
|
|
|
+
|
|
|
|
+import java.util.List;
|
|
|
|
+
|
|
|
|
+@Controller
|
|
|
|
+@RequestMapping("/roleRest")
|
|
|
|
+public class RoleRestController {
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private RoleService roleService;
|
|
|
|
+
|
|
|
|
+ // localhost:8080/springmvc_demo/roleRest/list
|
|
|
|
+ @GetMapping("/list")
|
|
|
|
+ public String list(Model model) {
|
|
|
|
+ List<Role> roles = roleService.queryRoles();
|
|
|
|
+ model.addAttribute("roleList", roles);
|
|
|
|
+ // WEB-INF/templates/rest/list.html
|
|
|
|
+ return "rest/list";
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // localhost:8080/springmvc_demo/roleRest/toAdd
|
|
|
|
+ @GetMapping("/toAdd")
|
|
|
|
+ public String toAdd() {
|
|
|
|
+ // WEB-INF/templates/rest/add.html
|
|
|
|
+ return "rest/add";
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // localhost:8080/springmvc_demo/role/add
|
|
|
|
+ // localhost:8080/springmvc_demo/roleRest post
|
|
|
|
+ @PostMapping()
|
|
|
|
+ public String add(Role role) {
|
|
|
|
+ roleService.add(role);
|
|
|
|
+ // localhost:8080/springmvc_demo/roleRest/list
|
|
|
|
+ return "redirect:/roleRest/list";
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // localhost:8080/springmvc_demo/roleRest/toUpdate/1
|
|
|
|
+ // @PathVariable 和 @RequestParam
|
|
|
|
+ @GetMapping("/toUpdate/{id}")
|
|
|
|
+ public String toUpdate(@PathVariable("id") int id, Model model) {
|
|
|
|
+ Role role = roleService.queryRoleById(id);
|
|
|
|
+ model.addAttribute("role", role);
|
|
|
|
+ // WEB-INF/templates/rest/update.html
|
|
|
|
+ return "rest/update";
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // localhost:8080/springmvc_demo/role/update
|
|
|
|
+ // localhost:8080/springmvc_demo/roleRest put
|
|
|
|
+ // <input type="hidden" name="_method" value="put">
|
|
|
|
+ @PutMapping()
|
|
|
|
+ public String update(Role role) {
|
|
|
|
+ System.out.println("role: " + role);
|
|
|
|
+ roleService.update(role);
|
|
|
|
+ // localhost:8080/springmvc_demo/roleRest/list
|
|
|
|
+ return "redirect:/roleRest/list";
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // localhost:8080/springmvc_demo/role/delete
|
|
|
|
+ // localhost:8080/springmvc_demo/roleRest/1 delete
|
|
|
|
+ // <input type="hidden" name="_method" value="put">
|
|
|
|
+ @DeleteMapping("/{id}")
|
|
|
|
+ public String delete(@PathVariable("id") int id) {
|
|
|
|
+ roleService.delete(id);
|
|
|
|
+ return "redirect:/roleRest/list";
|
|
|
|
+ }
|
|
|
|
+}
|