03-springmvc-Obtaining user request data

Source: Internet
Author: User

one, processing Requet URI part: @PathVariable
    • @PathVariable is a new feature of Spring3.0

    • by @PathVariable You can bind a placeholder parameter in a URL to an incoming parameter in a controller processing method. The source code is as follows

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M02/77/3F/wKiom1Zlki_RxA4FAAAhNthU6dI636.png " border= "0"/>

    • The {XXX} point character in the URL, bound to the incoming parameter in the action method by @pathvariable ("xxx")

1. Testing
    • Controller

@RequestMapping ("/testpathvariable/{id}") Public String testpathvariable (@PathVariable ("id") of the Integer ID) { System.out.println ("pathvariable" +id); return "Success";}
    • Request

<a href= "REQUESTDATA/TESTPATHVARIABLE/2" > Test @PathVariable </a>
2. Summary
    • @RequestMapping ("/testpathvariable/{id}") can also be placed above the class, as shown below

@[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
    • The request header, which contains a number of properties that the server can learn about the client (can be viewed through Firebug)

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M01/77/3D/wKioL1Zlkp6iBzvhAAAkm7kwm0U601.png " border= "0"/>

    • The property value of the request header can be bound to the incoming parameter of the processing method through @requestheader. The source code is as follows

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M01/77/3D/wKioL1Zlkp_gcnApAACRJqJopeo050.png " Border= "0" height= "518"/>

1.2 Testing
    • Controller class

@RequestMapping ("/testrequestheader") public String Testrequestheader (@RequestHeader (value= "Accept-language") String al) {System.out.println ("Accept-language:" +al); return "Success";}
    • Request

<a href= "Requestdata/testrequestheader" > Test @requestheader</a>
2. Use @cookievalue to bind the cookie value in the request 2.1, the knowledge point
    • View Cookies

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; "/>

    • @CookieValue can handle a cookie value that is passed into the binding method

2.2 Testing
    • Test class

@RequestMapping ("/testcookievalue") public string Testcookievalue (@CookieValue (value= "Jsessionid") string jsession) {System.out.println ("Jsessionid:" +jsession); return "Success";}
    • Request

<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
    • Source

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; "/>

    • At the incoming parameter of the processing method, the request parameter can be passed to the method using @requestparam

      • Value: Name of parameter

      • Required: Whether it must, by default, True, indicates that the parameter must contain the corresponding parameter, and throws an exception if it does not exist.

      • DefaultValue: Default value for Request parameters

1.2. Testing
    • Controller class

@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";}
    • Request

<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
    • Bean

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+"] ";}}
    • Controller

@RequestMapping ("/testpojo") public String Testpojo (user user) {System.out.println ("Testpojo:" +user); return "success ";}
    • Request

<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
    • Controller

@RequestMapping ("/testservletapi") public String Testservletapi (HttpServletRequest request,httpservletresponse Response) {System.out.println ("Testservletapi:" +request+ "," +response "; return" Success ";}
    • Request

<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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.