Spring MVC URL Submission parameters and get parameters

Source: Internet
Author: User
Tags getmessage

The construction of the demonstration environment reference the previous article, this article is important to describe the URL to submit parameters and get the parameters of the specific operation and implementation.

"Reproduced use, please specify the source: 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:

/** * Spring MVC URL Submission Parameters * @param name * @return */@RequestMapping ("/param") public Modelandview GetInfo (@RequestParam ("Nam E ") string name) {String str = name +" Spring MVC example "; return new Modelandview (" message "," str ", str);}


         access url format: param?name=hoking (Get mode . This is a very common way to submit. Using annotations @RequestParam Binding request Parameters a to variable a when request parameter Span style= "Font-family:times new Roman" >a does not exist when an exception occurs You can set the property required=false to resolve 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 URIfor a set of resources, such as/ HTTP/ example.com/resources/ the URIof 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:

/** * Spring MVC supports restful-style URL parameters *  * @return */@RequestMapping ("/index/{username}") Public String GetMessage (@ Pathvariable ("username") String username) {System.out.println (username); return "message";}


The @PathVariableis used above. The difference between pathvariable and Requestparam is.

When using the @RequestMapping URI template -style mapping, which is someurl/{paramid}, the paramid can be @Pathvariable the annotation binds the value it passes over to the parameter of the method.

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.

"Reproduced use, please specify the source: http://blog.csdn.net/mahoking "

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.