PackageCom.springmvc.controller;ImportCom.springmvc.model.UserInfo;ImportOrg.springframework.core.io.ClassPathResource;ImportOrg.springframework.core.io.Resource;ImportOrg.springframework.stereotype.Controller;Importorg.springframework.util.FileCopyUtils;Importorg.springframework.web.bind.annotation.*;Importorg.springframework.web.context.request.WebRequest;ImportOrg.springframework.web.servlet.ModelAndView;Importjava.io.IOException;ImportJava.io.OutputStream;/*** <p>http Request Data Binding </p> * <p> * binding via annotations: * [email protected]> bind request parameter * [email protected]> the variable in the binding URL * [Email protected]> bind request Header parameter * [email protected]> bind cookie value * Use servlet API object as parameter: * 1.HttpServletRequest * 2.HttpServ Letresponse * 3.HttpSession * Use Spring's servlet API proxy class * 1.WebRequest * Use IO object as parameter * 1.java.io.inputstream/java.io.reader * 2.java.io.outputstream/java.io.write * Other: Local * <p> * <p> * </p> * <p> * Created by liaody on 2 017/4/30. */@Controller ("Databindcontroller") Public classDatabindcontroller {/*** Default constructor*/ PublicDatabindcontroller () {}/*** <p> @RequestParam bind request parameters. </p> * The parameter is not required in the <p>requestmapping qualification, and automatic binding is implemented when the parameter contains the name of the specified parameter. </p> * <p> * Note that @requestparam has three parameters, default value, required (true by default), defaultvalue default (deprecated). * An exception is thrown when the corresponding parameter does not exist in the request. If the parameter is not required, you can set required to False (UserEmail in the example). * </p> * <p> * Request URL:http://localhost: 8080/requestparambind?username=ldy&userpassword=123 * </p> *@return */@RequestMapping (Value= "/requestparambind") PublicModelandview Requestparambind (@RequestParam ("UserName") string userName, @RequestParam ("UserPassword") string UserPassword, @RequestParam (value = "UserEmail", required =false) String useremail) {modelandview mv=NewModelandview (); System.out.println ("Username, UserPassword, useremail" in request parameters using @requestparam binding); System.out.println ("UserName:" +userName); System.out.println ("UserPassword:" +UserPassword); Mv.setviewname ("/success"); Mv.addobject ("Message", "Username/userpassword" + userName + "/" +UserPassword); returnMV; } /*** <p> @PathVariable the variable in the binding URL. </p> * <p> Example: * value= "/user/**\/{userid}, matching/user/aa/123,/USER/AA/BB/ABC,/USER/AA/BB/CC/DDD * < /p> * <p> * Request URL:http://localhost: 8080/user/aa/123,http://localhost: 8080/USER/AA/BB/ABC,http://localhost: 8080/USER/AA/BB/CC/DDD * </p> * *@return */@RequestMapping (Value= "User/**/{userid}") PublicModelandview requestparambindvariable (@PathVariable ("UserId") String userId) {modelandview mv=NewModelandview (); System.out.println ("Binding parameters with @pathvariable annotations userid:" +userId); Mv.addobject ("Message", "Binding parameter UserID" +userId); Mv.setviewname ("/success"); returnMV; } /*** <p> use command/form object Binding </p> * <p> * The so-called command/Form object does not need to implement any interface, only a POJO,SPRINGMVC with several properties as follows: "HTTP request parameters The rule of the name = command/Form object's property name, which automatically binds the request data, supports cascading property names, and automatically transforms the base data type. * </p> * <p> * Request URL:http://localhost: 8080/requestparambindpojo?username=yy&userpassword=zz&userloginflag=1&userloginflag=1&[ Email protected] * </p> * <p> * username=yy&userpassword=zz&userloginflag=1&userloginf Lag=1&[email protected]> automatically mapped to UserInfo type instance userinfo * </p> * *@return */@RequestMapping (Value= "/requestparambindpojo") PublicModelandview Requestparambindpojo (UserInfo UserInfo) {modelandview mv=NewModelandview (); System.out.println (Userinfo.tostring ()); Mv.setviewname ("/success"); Mv.addobject ("Message", "command/Form Object binding" +userinfo.tostring ()); returnMV; } /*** <p> @RequestHeader Binding request Header parameters, @CookieValue-binding cookie Value </p> * <p> * Request URL:http://localhost: 8080/requestparambindcookieandheader * </p> * *@return */@RequestMapping (Value= "/requestparambindcookieandheader") PublicModelandview Requestparambindcookieandheader (@CookieValue ("Jsessionid") String sessionId, @RequestHeader ("Accept-language") String language) {Modelandview mv=NewModelandview (); System.out.println ("SessionID:" +sessionId); System.out.println ("Language:" +language); Mv.setviewname ("/success"); Mv.addobject ("Message", "sessionId" +sessionId); Mv.addobject ("Message2", "Language:" +language); returnMV; } /*** <p> use spring's servlet API proxy class/p> * <p> * Spring MVC in Org.spirngframework.web.context.reque The St package defines a number of proxy servlet native APIs, * such as WebRequest and Nativewebrequest, which also allow access to any information from the requesting object as an entry to the processing class. * </p> * <p> * Request URL:http://localhost: 8080/requestparambindwebrequest?user=liaody * </p> * *@return */@RequestMapping (Value= "/requestparambindwebrequest") PublicModelandview requestparambindwebrequest (WebRequest request) {Modelandview MV=NewModelandview (); String UserName= Request.getparameter ("UserName"); System.out.println ("Get the parameter username by using the Spring servlet API proxy class to access the native Servlet API:" +userName); Mv.setviewname ("/success"); Mv.addobject ("Message", "UserName" +userName); returnMV; } /*** <p> use IO object as parameter/p> * <p> * Spring MVC allows the controller to use Java.io.inputstream/java.io.reader and Java for processing. Io. Outputstream/java.io.writer as the method's entry. * Spring MVC acquires the outputstream/writer of the ServletRequest Inputstream/reader or servletresponse and then passes it to the controller by the type matching method. * Enter the parameter. * </p> * <p> * Request URL:http://localhost: 8080/requestparambindio * </p> * *@return */@RequestMapping (Value= "/requestparambindio") Public voidRequestparambindio (OutputStream OS)throwsIOException {Resource res=NewClasspathresource ("/image.jpg");//read the picture file under the ClasspathFilecopyutils.copy (Res.getinputstream (), OS);//write a picture to the output stream }}
Spring MVC HTTP request data binding