Delete transmission mode in spring MVC restful

Source: Internet
Author: User

In spring restful, when the browser does not support the put and delete transmission protocols, you can add a hidden field to the form. The hidden name attribute is: _ method, for example:

 
<Form action = "Update" method = "Post">... <input type = "hidden" name = "_ method" value = "put"/> </form>

However, the default method filter (Org. springframework. Web. Filter. hiddenhttpmethodfilter) in spring only judges the post transmission protocol.

Custom get support

Public class myhiddenhttpmethodfilter extends hiddenhttpmethodfilter {private string methodparam = default_method_param; Public void setmethodparam (string methodparam) {assert. hastext (methodparam, "'methodparam' must not be empty"); this. methodparam = methodparam;} @ overrideprotected void dofilterinternal (httpservletrequest request, httpservletresponse response, filterchain) throws servletexception, ioexception {string paramvalue = request. getparameter (methodparam); string _ method = request. getmethod (); If (stringutils. haslength (paramvalue) {string method = paramvalue. touppercase (locale. english); Boolean B = ("Post ". equals (_ method) & "put ". equalsignorecase (method) | ("get ". equals (_ method) & "delete ". inclusignorecase (method); If (B) {httpservletrequest wrapper = new httpmethodrequestwrapper (request, method); filterchain. dofilter (wrapper, response);} else {}} else {filterchain. dofilter (request, response) ;}} Private Static class httpmethodrequestwrapper extends httpservletrequestwrapper {private final string method; Public httpmethodrequestwrapper (httpservletrequest request, string method) {super (request); this. method = method ;}@ overridepublic string getmethod () {return this. method ;}}}

When the delete protocol is used, the request uses get and the parameter _ method = Delete is added. When the put protocol is used, the request uses post and then adds _ method = put.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.