REST中的
GET-->查询
POST-->添加
PUT-->修改
DELETE-->删除

浏览器代码
//



//

// ......

//


服务器代码
// @RequestMapping(value = "/rest/{id}",method = RequestMethod.DELETE)

// public String testrestDELETE(@PathVariable int id, Model model){

// model.addAttribute("msg","delete请求"+id);

// return SUCCESS;

// }

// @RequestMapping(value = "/rest/{id}",method = RequestMethod.PUT)

// public String testrestPUT(@PathVariable int id,Model model){

// model.addAttribute("msg","put请求"+id);

// return SUCCESS;

// }

// @RequestMapping(value = "/rest/{id}",method = RequestMethod.POST)

// public String testrestPOST(@PathVariable int id,Model model){

// model.addAttribute("msg","post请求"+id);

// return SUCCESS;

// }

// @RequestMapping(value = "/rest/{id}",method = RequestMethod.GET)

// public String testrestDELETE(@PathVariable int id, ModelMap modelMap){

// modelMap.addAttribute("msg","get请求"+id);

// return SUCCESS;

// }

而浏览器form表单只支持GET与POST请求,而DELETE、PUT等method并不支持,所以spring3.0添加了一个过滤器,可以将这些请求转换为标准的http方法,使得支持GET、POST、PUT与DELETE请求,该过滤器为HiddenHttpMethodFilter。
HiddenHttpMethodFilter中有一个方法叫做doFilterInternal方法

protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)

throws ServletException, IOException {

String paramValue = request.getParameter(this.methodParam);//此方法this.methodParam属性值是一个常量 DEFAULT_METHOD_PARAM ,常量值为 "_method" ,获得客户端的参数,参数名为_mehtod

if ("POST".equals(request.getMethod()) && StringUtils.hasLength(paramValue)) { //如果当前的请求方式为post,并且paramValue字符串不为空

String method = paramValue.toUpperCase(Locale.ENGLISH); //将method转换成大写

HttpServletRequest wrapper = new HttpMethodRequestWrapper(request, method); //HttpMethodRequestWrapper:此方法作用:将POST修改成传入进来的method的值

filterChain.doFilter(wrapper, response);

}

else {

filterChain.doFilter(request, response);

}

}

HiddenHttpMethodFilter的父类是OncePerRequestFilter,它继承了父类的doFilterInternal方法,工作原理是将jsp页面的form表单的method属性值在doFilterInternal方法中转化为标准的Http方法,即GET,、POST、 HEAD、OPTIONS、PUT、DELETE、TRACE,然后到Controller中找到对应的方法。例如,在使用注解时我们可能会在Controller中用于@RequestMapping(value = "list", method = RequestMethod.PUT),所以如果你的表单中使用的是

,那么这个表单会被提交到标了Method="PUT"的方法中。

使用HiddenHTTPMethodFilter过滤器时,需要手动配置过滤器


HiddenHttpMethodFilter

org.springframework.web.filter.HiddenHttpMethodFilter





HiddenHttpMethodFilter

/*



还需要符合以下条件
1.POST请求
2.参数为_method
如不符合条件,请求条件直接为浏览器的POST请求
若符合条件,那么经过转换之后,那么才算真正的请求方式,就是_method的值

posted on 2021-03-20 17:20  文种玉  阅读(135)  评论(0编辑  收藏  举报