Rest-style URLs. Take CRUD For example: add:/order POST Modify:/order/1 PUT update?id=1 get:/ORDER/1 get get?id=1 Delete:/ORDER/1 delete delet E?id=1,
The browser only supports post and get, you want to implement the delete and put methods, you need to use a filter hiddenhttpmethodfilter
1, configuring the filter
<Filter> <Filter-name>Hidden</Filter-name> <Filter-class>Org.springframework.web.filter.HiddenHttpMethodFilter</Filter-class> </Filter> <filter-mapping> <Filter-name>Hidden</Filter-name> <Url-pattern>/*</Url-pattern> </filter-mapping>
2, the client initiates the request
The filter uses this parameter of _method to determine what type of filter it is, so you need to add _method's hidden field to the front-end submission form, paying attention to using the Post method for submission
<formAction= "/REST/12"Method= "POST"> <inputtype= "hidden"name= "_method"value= "DELETE"> <inputtype= "Submit"value= "Delete"> </form> <formAction= "/REST/12"Method= "POST"> <inputtype= "hidden"name= "_method"value= "PUT"> <inputtype= "Submit"value= "Put"> </form> <formAction= "/REST/12"Method= "POST"> <inputtype= "Submit"value= "POST"> </form> <formAction= "/REST/12"Method= "Get"> <inputtype= "Submit"value= "Get"> </form>
3, the writing of the back-end controller
The controller uses requestmapping directly to specify the method.
@RequestMapping (value = "/rest/{id}", method =requestmethod.delete) PublicString Testrestdelete (@PathVariableintID, model model) {Model.addattribute ("MSG", "Delete request" +ID); returnSUCCESS; } @RequestMapping (Value= "/rest/{id}", method =requestmethod.put) PublicString Testrestput (@PathVariableintId,model Model) {Model.addattribute ("MSG", "Put request" +ID); returnSUCCESS; } @RequestMapping (Value= "/rest/{id}", method =requestmethod.post) PublicString Testrestpost (@PathVariableintId,model Model) {Model.addattribute ("MSG", "POST request" +ID); returnSUCCESS; } @RequestMapping (Value= "/rest/{id}", method =requestmethod.get) PublicString Testrestdelete (@PathVariableintID, Modelmap modelmap) {Modelmap.addattribute ("MSG", "Get Request" +ID); returnSUCCESS; }
Special attention!!!!!!!!!!!!!
Delete and post requests are not supported on TOMCAT8, so the above can only be performed on TOMCAT7
The TOMCAT8 runtime can enter the appropriate controller, but when the view rendering is returned, the exception page is reported because the two methods are not supported
Hiddenhttpmethodfilter request filtering for RESTful URLs