"Reprint: Http://blog.csdn.net/mahoking"
Normal URL Submission Parameters
The format URL is: url.do?param1=mahc¶m2=8888.00
You need to add the following method to the Hellocontroller object above:
1 /** 2 * Spring MVC URL Submission Parameters3 * @param name4 * @return5 */ 6@RequestMapping ("/param") 7 PublicModelandview GetInfo (@RequestParam ("name") (String name) {8 9String str = name +"Spring MVC Example"; Ten return NewModelandview ("message","Str", str); One}
The URL format for accessing the method is: param?name=hoking (get mode). This is a very common way to submit. @requestparam binding request parameter A to variable a when request parameter A does not exist an exception occurs, can be resolved by setting properties Required=false, for example: @RequestParam (value= "a", Required=false )。 As above, get the submitted parameters by name.
RESTful-style URL parameters
Next we look at the restful style. Typical application of the HTTP request method in RESTful Web services get PUT POST delete uri for a set of resources, such as http://example.com/resources/URI for a single resource, such as HTTP/ example.com/resources/142. For more information, please read the following article.
The specific implementation needs to add the following method in the Hellocontroller object above:
1 /** 2 * Spring MVC supports restful-style URL parameters3 * 4 * @return5 */ 6@RequestMapping ("/index/{username}") 7 PublicString GetMessage (@PathVariable ("username") String username) {8System. out. println (username); 9 return "message"; Ten}
The @pathvariable is used above. The difference between pathvariable and Requestparam is.
When using the @requestmapping URI template-style mapping, which is someurl/{paramid}, Paramid can bind the value passed to the method's parameters by @pathvariable annotations.
The URL format for accessing the method is: index/mahoking. @PathVariable is used to obtain the dynamic parameters in the request URL, it is very convenient. Mahoking is the dynamic value of the username.
The GetMessage () method above returns a String object that represents the page's jump address and does not contain the extension (suffix name). In this case, the message.jsp page.
Spring MVC URL Submission parameters and get parameters