Binding of HTTP request data
Binding with annotations
@RequestParam--bind Request parameter, @RequestHeader--bind request Header parameter, @CookieValue--the value of the bound cookie, @PathVariable--and the binding URL
Example:
@RequestMapping (value= "/handle2")
public string Handle2 (@CookieValue ("jsessionid") string sessionId,
@RequestHeader ("accept-language") String Accpetlanguage) {
...
}
@RequestParam has the following three Parameters.
Value: the name of the Parameter.
required: if required, The default is true, which means that the request must contain the corresponding parameter name and throws an exception if it does not exist.
Defaultvalue: default parameter name, when setting this parameter, automatically set required to False. This parameter is required for very few situations, and is not recommended for Use.
@RequestMapping (value= "/handle1")
public string Handle1 (@RequestParam ("userName") string UserName,) {
...
}
The above processing method, if the HTTP request does not contain the "userName" parameter, will produce an exception!! therefore, If you cannot guarantee the existence of a "userName" parameter, you must use:
@RequestParam (value = "userName", required = False)
Using Command/form object Bindings
The so-called Command/form object does not need to implement any interface, only a pojo with several properties. Spring MVC by:
"http Request Parameter name = property name of Command/form object"
rules, automatically binds request data, supports "cascading property names", and automatically converts basic types of Data.
@RequestMapping (value = "/handle14")
Public String handle14 (User user) {...}
Class user{
Private String userName;
username=xxx&password=yyy---> Private String password;
}
Using the Servlet API object as the entry parameter
In spring mvc, the controller class can be independent of any servlet API object, but spring MVC does not prevent us from using the Servlet Api's class as the processing Method's entry. It is worth noting that if the processing method returns the response using HttpServletResponse itself, The return value of the processing method is set to void .
@RequestMapping (value = "/handle21")
public void Handle21 (httpservletrequestrequest,httpservletresponseresponse) {
String username= webutils.findparametervalue (request, "userName");
Response.addcookie (new Cookie ("userName", userName));
}
Public String Handle24 (httpservletrequest request,
@RequestParam ("userName") String userName) {
...
Return "success";
}
Public String Handle23 (HttpSession session) {
Session.setattribute ("sessionId", 1234);
Return "success";
}
Using the Spring servlet API proxy class
Spring MVC defines several interfaces in the Org.springframework.web.context.request package that can proxy the native API classes of the servlet, such as WebRequest and nativewebrequest, which are also allowed as arguments to the processing class, through these proxy classes Any information that can access the requested Object.
@RequestMapping (value = "/handle25")
Public String Handle25 (WebRequest request) {
String userName = Request.getparameter ("userName");
Return "success";
}
Using the IO object as the entry parameter
Spring MVC allows the controller to be processed using Java.io.inputstream/java.io.reader and Java.io.outputstream/java.io.writer as the parameters of the method
Spring MVC acquires the outputstream/writer of the ServletRequest Inputstream/reader or servletresponse, and then passes to the Controller's processing method in the same way as the type Matches.
@RequestMapping (value = "/handle31")
public void Handle31 (outputstream os) throws ioexception{
Resource res = new Classpathresource ("/image.jpg");//read The picture file under the Classpath
Filecopyutils.copy (res.getinputstream (), Os);//write The picture to the output stream
}
The parameters of the controller processing method, in addition to supporting the above types of arguments, also support java.util.Locale, java.security.Principal, which can be obtained through the Servlet's httpservletrequest GetLocale () and Getuserprincipal () to get the corresponding Value. If the entry type of the processing method is locale or principal,spring MVC automatically obtains the corresponding object from the request object and passes the arguments to the processing method.
@RequestMapping (value = "/handle32")
public void Handle31 (locale locale) throws ioexception{
...
}
Httpmessageconverter<t>
In the case of Service-side processing, no additional processing is required except for the method signature using @requestbody/@ResponseBody or httpentity<t>/responseentity<t>. By httpmessageconverter, which is assembled in spring mvc, it has the ability to handle XML and Json.
Spring MVC (ii) Binding of HTTP request data