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:
?
| 1234567891011 |
/** * Spring MVC URL提交参数 * @param name * @return */ @RequestMapping(/param) public ModelAndView getInfo(@RequestParam(name) String name){ String str = name + Spring MVC示例; return new ModelAndView(message, str, str); } |
The URL format for accessing the method is: param?name=hoking (get mode). This is a very common way to submit. Bind request parameter A to variable a when the request parameter A does not exist, an exception occurs with the annotation @requestparam, and can be resolved by setting the property 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 the URI of a set of resources, such as http://example.com/resources/a URI for a single resource, such as www.2cto.com. For more information, please read the following article.
The specific implementation needs to add the following method in the Hellocontroller object above:
?
| 12345678910 |
/** * Spring MVC 支持RESTful风格的URL参数 * * @return */ @RequestMapping(/index/{username}) public String getMessage(@PathVariable(username) String username){ System.out.println(username); return message; } |
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