随笔 - 326,  文章 - 0,  评论 - 0,  阅读 - 16万

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   文种玉  阅读(140)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示