Spring MVC URL Submission parameters and get parameters

Source: Internet
Author: User

"Reprint: Http://blog.csdn.net/mahoking"

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:

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

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.