The difference between annotation @requestparam and @pathvariable in SPRINGMVC

Source: Internet
Author: User

@PathVariable binding URI Template variable value

@PathVariable is used to obtain the dynamic parameters in the request URL.

@PathVariable is used to map template variables in the request URL to the parameters of a functional processing method. Configure a relationship @RequestMapping for URLs and Methods ("Item/{itemid}")

/* @RequestMapping to map the request, that is, to specify which URL requests the controller can handle, similar to Struts's action request
* @responsebody indicates that the return result of the method is written directly to the HTTP response body
* Generally used when retrieving data asynchronously, after using @requestmapping, the return value usually resolves to the jump path, plus @responsebody returns the result will not be resolved to the jump path, but is written directly to the HTTP response *body.
* For example, asynchronously fetching JSON data, plus @responsebody, will return the JSON data directly. *
* @Pathvariable annotation binds the value it passes over to the parameter of the method
* Used to map the template variables in the request URL to the parameters of the function processing method, that is, the variables in the URI template are taken as parameters
*/
@ResponseBody
Public Tbitem GetItemByID (@PathVariable Long itemId) {

1@RequestMapping ("/zyh/{type}")2    PublicString Zyh (@PathVariable (value = "type")intTypethrowsunsupportedencodingexception {3String url = "http://wx.diyfintech.com/zyhMain/" +type;4     if(Type! = 1 && Type! = 2) {5       Throw NewIllegalArgumentException ("Parameter Error");6     }7String encodeurl = Urlencoder.encode (URL, "Utf-8");8String RedirectURL = Messageformat.format (Oauth_url, Wxconfig.zyhappid, Encodeurl, "Snsapi_userinfo", UUID.randomUUID (). toString (). Replace ("-", ""));9     return"Redirect:" +RedirectURL;Ten}

There are two main ways to get parameters in the SPRINGMVC background control layer:

One is Request.getparameter ("name"), and the other is to get directly with annotations @requestparam

This note is mainly about this @RequestParam

Let's take a look at the main parameters of the @requestparam annotation:

Value: The parameter name, that is, the parameter name of the request parameter, such as username indicates the parameter area of the request name is username The value of the argument will be passed in;

Required: Whether must, the default is true, indicates the request must have the corresponding parameter, otherwise will report 404 error code;

DefaultValue: Default value, which means that if the request does not have a parameter with the same name, for example:

Public list<easyuitreenode> Getitemtreenode (@RequestParam (value= "id", defaultvalue= "0") long ParentID)

1 @Controller2@RequestMapping ("/WX")3  Public classWxcontroller {4 5 @Autowired6     PrivateWxservice Wxservice;7     Private Static FinalLog log= Logfactory.getlog (Wxcontroller.class);8 9@RequestMapping (value = "/service", method =requestmethod.get)Ten      Public voidacceptwxvalid (@RequestParam string signature, @RequestParam string timestamp, @RequestParam string nonce, One@RequestParam String Echostr, httpservletresponse response)throwsIOException { APrintWriter out =Response.getwriter (); -         if(signutil.checksignature (signature, timestamp, nonce)) { - Out.print (ECHOSTR); the}Else -Out.print ("Fail"); - Out.flush (); - out.close (); +}

The difference between annotation @requestparam and @pathvariable in SPRINGMVC

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.