@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