Spring using form form to implement put, delete commits

Source: Internet
Author: User

The need for put, delete commits is essential in the rest service, but many of the current viewers do not support it. Therefore, some additional processing is required before using rest.

The specific solutions are as follows:

1, add a filterfirst. This filter is the key to achieving this function. Open web. XMLand add the following code (to be placed first):

Copy Code

 <Filter>        <Filter-name>Httpmethodfilter</Filter-name>        <Filter-class>Org.springframework.web.filter.HiddenHttpMethodFilter</Filter-class>    </Filter>    <filter-mapping>        <Filter-name>Httpmethodfilter</Filter-name>        <Url-pattern>/*</Url-pattern>    </filter-mapping>

This is going to intercept a method with _method .

2, front-end request modification

Standard Rest Delete requests Ajax notation:

$.ajax ({          "delete",                Url:url,                data: {"contentId": id},                function  (data) {                    if (data.status = = 0) {                        alert ("Success! ");                        Location.reload ();                     Else {                        alert ("The operation failed! "+ Data.reason);}}            );

This is the ideal way to request. However , the Delete method is not supported by the browser, so it should be changed to the following:

$.ajax ({                "post",                Url:url,                data: {"_method": "Delete"},                  function  (data) {                    if (data.status = = 0) {                        alert ("Success! ");                        Location.reload ();                     Else {                        alert ("The operation failed! "+ Data.reason);}}            );

pay attention to the marked Red section. Typeto bePost, because if theGetwords,Datawill be ignored in the parameters. parameter, add_method, TellSpringthis isDeleterequest. If this is the form, add a hiddeninput,nameis a_methodcan be. This allows you to use it across browsersRestinterface.

Spring using form form to implement put, delete commits

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.