SpringMVC 模拟 PUT 提交和 DELETE 方式的提交

步骤 1:在 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>

步骤 2 :编写控制器

@RequestMapping("/rest")
@Controller
public class RestController {


    @RequestMapping(value="/form")
    public ModelAndView goForm(){
        ModelAndView mav = new ModelAndView("/rest/form");
        return mav;
    }


    @RequestMapping(value="/get",method=RequestMethod.GET)
    public String testGet(){
        System.out.println("rest 的 get 请求。。。");
        return "/rest/get";
    }

    @RequestMapping(value="/post",method=RequestMethod.POST)
    public String testPost(){
        System.out.println("rest 的 post 请求。。。");
        return "/rest/post";
    }

    @RequestMapping(value="/put",method=RequestMethod.PUT)
    public String testPut(){
        System.out.println("rest 的 put 请求。。。");
        return "/rest/put";
    }

    @RequestMapping(value="/delete",method=RequestMethod.DELETE)
    public String testDelete(){
        System.out.println("rest 的 delete 请求。。。");
        return "/rest/delete";
    }
}

步骤 3 :编写表单模拟 PUT 和 DELETE 方式提交

说明: PUT 和 DELETE 方式 ,在写表单提交的时候还是发送 post 请求,但是要附加一个隐藏的文本域,name 的值固定为 “_method”,value 写 “PUT” 或者 “DELETE”。

<body>
    <form action="${pageContext.request.contextPath}/rest/get" method="get">
        <input type="submit" value="get 方式提交">
    </form>

    <form action="${pageContext.request.contextPath}/rest/post" method="post">
        <input type="submit" value="post 方式提交">
    </form>

    <form action="${pageContext.request.contextPath}/rest/put" method="post">
        <input type="hidden" name="_method" value="PUT">
        <input type="submit" value="put 方式提交">
    </form>

    <form action="${pageContext.request.contextPath}/rest/delete" method="post">
        <input type="hidden" name="_method" value="DELETE">
        <input type="submit" value="delete 方式提交">
    </form>
</body>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值