实现原理:检测请求参数中是否包含 _method 参数,如果包含则获取该参数的值,判断是哪种操作后完成请求类型的转换,然后继续传递。
1、在 form 表单中添加隐藏域标签,name="_method",value=“PUT”/“DELETE”。
<form action="/rest/put" method="post"> <input type="hidden" name="_method" value="PUT"/> <input type="submit"/> </form> <form action="/rest/delete" method="post"> <input type="hidden" name="_method" value="DELETE"/> <input type="submit"/> </form>2、web.xml 配置 HiddenHttpMethodFilter
<filter> <filter-name>HiddenHttpMethodFilter</filter-name> <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> </filter> <filter-mapping> <filter-name>HiddenHttpMethodFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>3、RestHandler
package com.southwind.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; @Controller @RequestMapping("/rest") public class RestHandler { @RequestMapping(value = "/put",method = RequestMethod.PUT) @ResponseBody public String rest(){ System.out.println("rest"); return "PUT"; } @RequestMapping(value = "/delete",method = RequestMethod.DELETE) @ResponseBody public String delete(){ System.out.println("delete"); return "DELETE"; } }1、创建实体类
package com.southwind.entity; public class Course { private Integer id; private String name; private Double price; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Double getPrice() { return price; } public void setPrice(Double price) { this.price = price; } public Course(Integer id, String name, Double price) { this.id = id; this.name = name; this.price = price; } }2、CourseRepository
package com.southwind.repository; import com.southwind.entity.Course; import org.springframework.stereotype.Repository; import java.util.Collection; import java.util.HashMap; import java.util.Map; @Repository public class CourseRepository { private static Map<Integer, Course> map; static { map = new HashMap(); map.put(1,new Course(1,"Java",6000d)); map.put(2,new Course(2,"C++",6000d)); map.put(3,new Course(3,"Python",6000d)); } public void saveOrUpdate(Course course){ map.put(course.getId(),course); } public Collection<Course> findAll(){ return map.values(); } public Course findById(Integer id){ return map.get(id); } public void deleteById(Integer id){ map.remove(id); } }3、CourseHandler
package com.southwind.controller; import com.southwind.entity.Course; import com.southwind.repository.CourseRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import org.springframework.web.servlet.ModelAndView; @Controller @RequestMapping("/course") public class CourseHandler { @Autowired private CourseRepository courseRepository; // @RequestMapping(value = "/findAll",method = RequestMethod.GET) @GetMapping("/findAll") public ModelAndView findAll(){ ModelAndView modelAndView = new ModelAndView("course"); modelAndView.addObject("list",courseRepository.findAll()); return modelAndView; } @DeleteMapping("/deleteById/{id}") public String deleteById(@PathVariable("id") Integer id){ courseRepository.deleteById(id); return "redirect:/course/findAll"; } @GetMapping("/findById/{id}") public ModelAndView findById(@PathVariable("id") Integer id){ ModelAndView modelAndView = new ModelAndView("update"); modelAndView.addObject("course",courseRepository.findById(id)); return modelAndView; } @PutMapping("/update") public String update(Course course){ courseRepository.saveOrUpdate(course); return "redirect:/course/findAll"; } @PostMapping("/add") public String add(Course course){ courseRepository.saveOrUpdate(course); return "redirect:/course/findAll"; } }