步骤 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>