38.HiddenHttpMethodFilter

文章介绍了SpringMVC框架中如何使用HiddenHttpMethodFilter处理POST请求转换为DELETE或PUT请求,以及在web.xml中配置和在CRUD操作中应用该过滤器的例子。
摘要由CSDN通过智能技术生成

HiddenHttpMethodFilter

由于浏览器只支持发送get和post方式的请求,SpringMVC 提供了 HiddenHttpMethodFilter 帮助我们将 POST 请求转换为 DELETE 或 PUT 请求

HiddenHttpMethodFilter 处理put和delete请求的条件:

  • 当前请求的请求方式必须为:method = "post"

  • 当前请求必须传输请求参数:name = "method"

  • 通过规定值的方式设置请求:value = "put/delete"

满足以上条件,HiddenHttpMethodFilter 过滤器就会将当前请求的请求方式转换为请求参数_method的值,因此请求参数_method的值才是最终的请求方式

添加过滤器

HiddenHttpMethodFilter 过滤器添加到web.xml

    <!--将 POST 请求转换为 DELETE 或 PUT 请求-->
	<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>

注:目前为止,SpringMVC中提供了两个过滤器:CharacterEncodingFilterHiddenHttpMethodFilter

在web.xml中注册时,必须先注册CharacterEncodingFilter,再注册HiddenHttpMethodFilter

原因:

  • CharacterEncodingFilter 中通过request.setCharacterEncoding(encoding) 方法设置字符集

    • request.setCharacterEncoding(encoding) 方法要求前面不能有任何获取请求参数的操作
  • HiddenHttpMethodFilter 恰恰有一个获取请求方式的操作:(所以需要在后面)

    • 	String paramValue = request.getParameter(this.methodParam);
      

CRUD

index.html

    <a th:href="@{/user}">查询所有用户信息</a>

    <a th:href="@{/user/2}">查询id为2用户信息</a><br>

    <form th:action="@{/user}" method="post">
        <input type="submit" value="添加用户信息">
    </form><br>

    <!--提交方式设置为post-->
    <form th:action="@{/user/2}" method="post">
        <!--用为不需要用户进行操作,一般将类型设置为"hidden"-->
        <input name="_method" value="put" type="hidden">
        <input type="submit" value="修改用户信息">
    </form><br>

    <form th:action="@{/user/2}" method="post">
        <input name="_method" value="delete" type="hidden">
        <input type="submit" value="删除用户信息">
    </form><br>

RESTfulController.java

package com.atguigu.SpringMVC.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
public class RESTfulController {

    //@RequestMapping(value = "/user",method = RequestMethod.GET)
    @GetMapping("/user") //相当于@RequestMapping加method = RequestMethod.GET
    public String testSelectAll(){
        System.out.println("查询所有用户信息-->/user-->GET");
        return "success";
    }

    @GetMapping("/user/{id}")
    public String testSelectOne(@PathVariable Integer id){
        System.out.println("查询id为"+id+"的用户信息-->/user/2-->post");
        return "success";
    }

    //地址虽然一样,但是系统可以通过发送的请求方式不同选择不同的方法
    @PostMapping("/user")
    public String testInsert(){
        System.out.println("添加用户信息-->/user-->post");
        return "success";
    }

    //处理PUT请求
    @PutMapping("/user/{id}")
    public String testUpdate(@PathVariable Integer id){
        System.out.println("修改id为"+id+"的用户信息-->/user/2-->put");
        return "success";
    }

    //处理DELETE请求
    @DeleteMapping("/user/{id}")
    public String testDelete(@PathVariable Integer id){
        System.out.println("删除id为"+id+"的用户信息-->/user/2-->delete");
        return "success";
    }
}

可以成功发送put和delete请求,控制器可以成功接收到请求和参数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值