Spring MVC URL Submission parameters and get parameters

Source: Internet
Author: User

Normal URL Submission Parameters

The format URL is: url.do?param1=mahc&param2=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

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.