one, processing Requet URI part: @PathVariable
650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M02/77/3F/wKiom1Zlki_RxA4FAAAhNthU6dI636.png " border= "0"/>
1. Testing
@RequestMapping ("/testpathvariable/{id}") Public String testpathvariable (@PathVariable ("id") of the Integer ID) { System.out.println ("pathvariable" +id); return "Success";}
<a href= "REQUESTDATA/TESTPATHVARIABLE/2" > Test @PathVariable </a>
2. Summary
@[email protected] ("/owners/{ownerid}") public class Relativepathuritemplatecontroller {@RequestMapping ("/pets/{ Petid} ") public void Findpet (@PathVariable string ownerid, @PathVariable string petid, model model) {//Implement ation omitted}}
then it corresponds to the request:/OWERS/1/PETS/2
The above code binds the value of the variable ownerid in the URI template and the value of the Petid to the parameter of the method. If the variable names in the method parameter names and URI template that need to be bound are inconsistent, you need to specify the name in the URI template in @pathvariable ("name").
Second,handling the Request header section@RequestHeader, @CookieValue
1. @RequestHeader
1.1 Knowledge points
650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M01/77/3D/wKioL1Zlkp6iBzvhAAAkm7kwm0U601.png " border= "0"/>
650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M01/77/3D/wKioL1Zlkp_gcnApAACRJqJopeo050.png " Border= "0" height= "518"/>
1.2 Testing
@RequestMapping ("/testrequestheader") public String Testrequestheader (@RequestHeader (value= "Accept-language") String al) {System.out.println ("Accept-language:" +al); return "Success";}
<a href= "Requestdata/testrequestheader" > Test @requestheader</a>
2. Use @cookievalue to bind the cookie value in the request 2.1, the knowledge point
650) this.width=650; "Src= " http://s3.51cto.com/wyfs02/M02/77/3F/wKiom1ZlkjHgBWWnAAAlK4VkN8M000 . png "border=" 0 "style=" font-size:12pt;line-height:1.5; "/>
2.2 Testing
@RequestMapping ("/testcookievalue") public string Testcookievalue (@CookieValue (value= "Jsessionid") string jsession) {System.out.println ("Jsessionid:" +jsession); return "Success";}
<a href= "Requestdata/testcookievalue" > Test @cookievalue</a>
Third,note for processing the body part of the request: @RequestParam, use the Pojo object to bind the requested parameter value;1. Bind request parameter value using @requestparam1.1 Knowledge points
650) this.width=650; "Src= " http://s3.51cto.com/wyfs02/M00/77/3F/wKiom1ZlkjKD14TVAACZWb-khL4936 . png "border=" 0 "height=" 473 "style=" font-size:12pt;line-height:1.5; "/>
1.2. Testing
@RequestMapping ("/testrequestparam") public String Testrequestparam (@RequestParam (value= "UserName", Required=false String UserName, @RequestParam (value= "age", defaultvalue= "int.") int age, @RequestParam (value= "password") string Password) {System.out.println ("UserName:" +username); System.out.println ("Age:" +age); System.out.println ("Password:" +password); return "Success";}
<a href= "Requestdata/testrequestparam" > Test @requestparam (No pass value) </a><br><a href= "requestData/ testrequestparam?username=imentors&password=123456 "> Test @requestparam (pass value) </a>
2, using Pojo object binding request parameter Value 2.1, knowledge point
Springmvc automatically fills the property values for the object by the request parameter name and the Pojo property name.
Cascading properties are supported. such as Dept.deptid, Dept.address.tel
2.2. Testing
public class Address {private string province;private string city; @Overridepublic string toString () {return "address [prov Ince= "+ Province +", city= "+ City +"] ";}}
public class User {private string Username;private string password;private string email;private int age; @Overridepublic St Ring toString () {return "User [username=" + Username + ", password=" + password+ ", email=" + email + ", age=" + Age + ", address= "+ address+"] ";}}
@RequestMapping ("/testpojo") public String Testpojo (user user) {System.out.println ("Testpojo:" +user); return "success ";}
<form action= "Requestdata/testpojo" method= "POST" > Username:<input type= "text" name= "username"/> <br/ > password:<input type= "password" name= "password"/> <br/> email:<input type= "text" name= "email"/ > <br/> age:<input type= "text" name= "age"/> <br/> city:<input type= "text" name= "address.city"/ > <br/> province:<input type= "text" name= "address.province"/> <br/> <input type= "Submit" value = "Submit"/> </form>
2.3 Summary
If a field name item in the Pojo object does not exist in the requested form parameter, then pojo the property is null after binding.
If there are items in the requested form parameter that do not contain fields in the Pojo object, the parameter is lost after binding.
If the data for the form item is automatically converted from string to the field type of the corresponding Pojo. However, if the type cannot be converted, an error is provided. If I enter age as DAFSDFA, then error.
Four,Using the Servlet API object as the entry parameter
1. using the Servlet API object as the entry parameter
1.1, Knowledge points
When using the Servlet API class as a parameter, Spring MVC automatically passes the Servlet API object corresponding to the Web layer to the processing method entry, and the processing method into the parameter can use the other qualified parameters at the same time, the position order has no special requirements.
If the processing method returns a response using HttpServletResponse itself, the processing method return value is set to void.
Spring MVC provides several interfaces to proxy servlet native API classes under the Org.springframework.web.context.request package
650) this.width=650; "Src= " http://s3.51cto.com/wyfs02/M02/77/3D/wKioL1ZlkqLCqWZ6AAA4XUVSYyU719 . png "border=" 0 "style=" font-size:12pt;line-height:1.5; "/>
1.2 Testing
@RequestMapping ("/testservletapi") public String Testservletapi (HttpServletRequest request,httpservletresponse Response) {System.out.println ("Testservletapi:" +request+ "," +response "; return" Success ";}
<a href= "Requestdata/testservletapi" > Test servletapi</a>
1.3 Summary
The Servlet native API can be used as a parameter to the target method, specifically supporting the following types
HttpServletRequest
HttpServletResponse
HttpSession
Java.security.Principal
InputStream
OutputStream
Reader
Writer
Follow us
The purpose of space division is to share knowledge and spread value. Follow us and get more information in a timely manner.
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/77/3D/wKioL1ZlkqPTt_mBAACHq5DKK3A992.png "border=" 0 "/ >
Donate US
If you agree with our results and feel that it is helpful to you, you are welcome to donate ^_^ to us.
650) this.width=650; "Src= " http://s3.51cto.com/wyfs02/M01/77/3F/wKiom1ZlkjbRo8ZVAAB9wjaIS_ E092.jpg "border=" 0 "style=" font-size:12pt;line-height:1.5; "/>
This article is from the "Division Space" blog, please be sure to keep this source http://imentors.blog.51cto.com/10946447/1720552
03-springmvc-Obtaining user request data