To put it first: I think restful style is just a style, not an advanced technical architecture, but a programming norm. In our application development process, we can find that more than 80% of operations are additions and deletions of the operation, restful is defined crud development specifications. Here's a comparison of restful URLs with traditional-style URLs.
Business operations
|
Traditional style URL
|
Traditional Request method
|
RESTful style URLs
|
RESTful request mode
|
New
|
/add
|
Get/post
|
/order
|
POST
|
Modify
|
/update?id=1
|
Get/post |
/order/1
|
PUT
|
Inquire
|
/get?id=1 |
Get/post |
/order/1 |
GET
|
Delete
|
/delete?id=1 |
Get/post |
/order/1 |
DELETE
|
Since we have been learning JSP, servlet, the use of the request is generally post or get, the browser is only supporting the post and get request mode, how to implement the put and delete requests? Don't worry, spring MVC provides a filter to implement put and delete requests. Let's take a look at this filter code, simply say the understanding of the gaze, the level is limited, not necessarily accurate: the browser currently only supports post and get requests, this filter can implement the POST request to put request or delete request, how is it implemented? With a normal post request, plus a hidden field, the name of the hidden field is _method, and when it finds a POST request and also has the _method property, it translates the request into a response HTTP request. It is also important to note that the filter is defined in the location of Web. XML because the filter needs to check the parameters of the post, so it needs to be defined after the file is uploaded to the filter, typically: Org.springframework.web.multipart.support.Multipar Tfilter.
package org.springframework.web.filter;import java.io.ioexception;import java.util.locale; import javax.servlet.filterchain;import javax.servlet.servletexception;import javax.servlet.http.httpservletrequest;import javax.servlet.http.httpservletrequestwrapper;import javax.servlet.http.httpservletresponse;import org.springframework.util.assert;import org.springframework.util.stringutils;import org.springframework.web.util.webutils;/** * {@link javax.servlet.Filter} that converts posted method * parameters into http methods, retrievable via * {@link httpservletrequest# GetMethod ()}. since browsers currently only * support get and Post, a common technique - used by the prototype * library, for instance - is to use a normal post with an additional * hidden form field ( {@code _method}) to pass the "Real" HTTP method along. * This filter reads that parameter and changes * the {@link Httpservletrequestwrapper#getmethod ()} return value accordingly. * * <p> the name of the request parameter defaults to {@code _method}, but can be * adapted via the {@link #setMethodParam (String) methodparam} property. * * <p><b>note: this filter needs to run after multipart processing in case of * a multipart POST request, due to its inherent need for checking a post * body parameter.</b> * so typically, put a spring * {@link org.springframework.web.multipart.support.multipartfilter} * <i>before</i > this hiddenhttpmethodfilter in your {@code web.xml} filter chain. * * @author Arjen Poutsma * @author Juergen Hoeller * @since 3.0 */public class hiddenhttpmethodfilter extends onceperrequestfilter { /** default method parameter: {@code _method} */ public static final String DEFAULT_METHOD_PARAM = "_ Method "; private string methodparam = default_method_param; /** * Set the parameter name to Look for http methods. * @see #DEFAULT_METHOD_PARAM */ public void setmethodparam (String methodparam) { assert.hastext (methodparam, "' Methodparam ' must not be empty "); this.methodparam = methodparam ; } @Override protected void Dofilterinternal (httpservletrequest request, Httpservletresponse response, filterchain filterchain) throws ServletException, IOException { HttpServletRequest requestToUse = request; if ("POST. Equals (Request.getmethod ()) && request.getattribute (Webutils.error_exception_attribute) == null) { String paramValue = Request.getparameter (This.methodparam); if (Stringutils.haslength (paramvalue)) { requesttouse = new httpmethodrequestwrapper (Request, paramvalue); } } filterchain.dofilter ( Requesttouse, response); } /** * simple {@link httpservletrequest} wrapper that returns the supplied method for * {@link httpservletrequest#getmethod ()}. */ private static class HttpMethodRequestWrapper extends Httpservletrequestwrapper { private final string method; public httpmethodrequestwrapper ( Httpservletrequest request, string method) { super (Request); this.method = method.touppercase (locale.english); } @Override public string getmethod () &NBSP;{&NBSP;&NBsp; return this.method; } }}
With the filter above, combined with the previous requestmapping method attribute, you can implement a put or delete request, see the following two specific examples
1. Take the put request as an example
The first step is to configure the filter in Web. xml
<!--Hiddenhttpmethodfilter can convert a POST request to a delete or put request, with the help of the _method property,--><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>
The second step: the definition of Form form, which is actually a post, defines a hidden field
<form action= "/RESTFUL/TESTPUT/1" method= "POST" > <!--POST request to a PUT request key--<input type= "hidden" name= "_method" value= "PUT"/> <p>put request </p> <input type= "Submit" value= "Submit"/></form>
The third step: the definition of the background method, defined as put, using annotation @pathvariable to get the parameters in the URL
@RequestMapping (value = "/testput/{id}", method = requestmethod.put) public String testput (@PathVariable ("id") int id) { SYSTEM.OUT.PRINTLN ("Test put function" + ID); return "greeting";}
This is a put request that implements a restful style.
Project Source code: Restfulcontroll.java
Https://git.oschina.net/acesdream/spring-mvc
This article is from "Ace's Dream" blog, please be sure to keep this source http://acesdream.blog.51cto.com/10029622/1905068
Third, the implementation of Spring MVC's restful style