<input type="hidden" name="_method" value="DELETE"/>
1.
RestfulController.java
1 package com.cn.restful; 2 3 import org.springframework.stereotype.Controller; 4 import org.springframework.ui.ModelMap; 5 import org.springframework.web.bind.annotation.*; 6 7 import java.util.ArrayList; 8 import java.util.Arrays; 9 import java.util.Iterator; 10 import java.util.List; 11 12 /** 13 * @Author hxy 14 * @Description Restful 页面控制器 15 * @Date 2023/9/15 15:19 16 * @Version 1.0 17 */ 18 @Controller 19 public class RestfulController { 20 private List<UserDto> userlist=new ArrayList<>(Arrays.asList( 21 new UserDto(1,"张三"), 22 new UserDto(2,"李四") 23 )); 24 25 // @RequestMapping(value = "/user/list",method = RequestMethod.GET) 26 @GetMapping("/user/list") 27 public String listUser(ModelMap modelMap){ 28 modelMap.addAttribute("msg",userlist); 29 return "result"; 30 } 31 32 @GetMapping("/user/{id}")//url地址动态绑定 33 public String getUser(@PathVariable("id") Integer id, ModelMap modelMap){ 34 for (UserDto user:userlist){ 35 modelMap.addAttribute("msg",user); 36 break; 37 } 38 return "result"; 39 } 40 41 @PostMapping("/user") 42 public String addUser(@ModelAttribute("msg") UserDto userDto){ 43 userlist.add(userDto); 44 return "result"; 45 } 46 47 @PutMapping("/user") 48 public String updateUser(@ModelAttribute("msg") UserDto user){ 49 for (UserDto userDto: userlist){ 50 if (userDto.getId().equals(user.getId())){ 51 userDto.setName(user.getName()); 52 break; 53 } 54 } 55 return "redirect:/toResult";//重定向 56 } 57 58 @DeleteMapping("/user/{id}") 59 public String delUser(@PathVariable("id") Integer id){ 60 //迭代器 61 Iterator<UserDto> iterator = userlist.iterator(); 62 while(iterator.hasNext()) { 63 UserDto user= iterator.next(); 64 if (user.getId().equals(id)){ 65 iterator.remove(); 66 } 67 } 68 return "redirect:/toResult"; 69 } 70 71 @GetMapping("/toResult") 72 public String result(ModelMap modelMap){ 73 modelMap.addAttribute("msg",userlist); 74 return "result"; 75 } 76 }
标签:架构,SpringMVC,resultful,userlist,user,id,import,UserDto,public From: https://www.cnblogs.com/hxysg/p/17742665.html