标签:work 隐藏 upd 获取 filter context value rect private
1.什么是RESTful
2.对应增删改查
注意:HTTP协议中并无PUT和DELETE的操作因此需在web.xml添加HiddenHttpMethodFilter用于识别PUT和DELETE操作
原理是:从POST操作中额外分出PUT和DELETE操作
<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> </web-app>
并且使用PUT操作和DELETE操作时需在jsp页面添加隐藏的input标签,以表示PUT或DELETE
如使用删除操作:
<form action="${pageContext.request.contextPath}/delete/${course.id}" method="post"> <button class="btn btn-danger btn-sm delete_btn" type="submit"> <input type="hidden" name="_method" value="DELETE"/> <%-- 应添加此input标签 --%> <span class="glyphicon glyphicon-trash">删除</span> </button> </form>
3.controller的定义:
package com.yzy.controller; import com.yzy.dao.courseDao; import com.yzy.entity.Course; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import org.springframework.web.servlet.ModelAndView; import javax.annotation.Resource; @Controller public class courseController { @Resource(name = "courseDao") private courseDao courseDao; @PostMapping("/add") public String add(Course course){ courseDao.add(course); return "redirect:/getAll"; //重定向回getAll } @GetMapping("/getAll") public ModelAndView getAll(){ ModelAndView modelAndView=new ModelAndView(); modelAndView.addObject("courses",courseDao.getAll()); modelAndView.setViewName("index"); return modelAndView; } @GetMapping("/getById/{id}") public ModelAndView updateBefore(@PathVariable int id){ //id从路径中的{id}获取 ModelAndView modelAndView=new ModelAndView(); modelAndView.addObject("course",courseDao.getById(id)); modelAndView.setViewName("edit"); return modelAndView; } @PutMapping("/update") public String update(Course course){ courseDao.update(course); return "redirect:/getAll"; } @DeleteMapping("/delete/{id}") public String delete(@PathVariable int id){ courseDao.delete(id); return "redirect:/getAll"; } }
HiddenHttpMethodFilter
标签:work 隐藏 upd 获取 filter context value rect private
原文地址:https://www.cnblogs.com/shouyaya/p/13040505.html