为什么Spring MVC中要通过"_method"来使用PUT和DELETE

HTML提交表单只支持GET和POST,所以要传递额外参数,Spring MVC的解决方案是通过添加隐藏域_method来指示PUT、DELETE。

18.2.4 HTTP Method Conversion

A key principle of REST is the use of the Uniform Interface. This means that all resources (URLs) can be manipulated using the same four HTTP method: GET, PUT, POST, and DELETE. For each methods, the HTTP specification defines the exact semantics. For instance, a GET should always be a safe operation, meaning that is has no side effects, and a PUT or DELETE should be idempotent, meaning that you can repeat these operations over and over again, but the end result should be the same. While HTTP defines these four methods, HTML only supports two: GET and POST. Fortunately, there are two possible workarounds: you can either use JavaScript to do your PUT or DELETE, or simply do a POST with the 'real' method as an additional parameter (modeled as a hidden input field in an HTML form). This latter trick is what Spring's HiddenHttpMethodFilter does. This filter is a plain Servlet Filter and therefore it can be used in combination with any web framework (not just Spring MVC). Simply add this filter to your web.xml, and a POST with a hidden _method parameter will be converted into the corresponding HTTP method request.


摘抄自https://docs.spring.io/spring-framework/docs/3.0.0.M3/reference/html/ch18s02.html

你可能感兴趣的:(为什么Spring MVC中要通过"_method"来使用PUT和DELETE)