Spring MVC url submits parameters and obtains parameters. springmvc

Source: Internet
Author: User

Spring MVC url submits parameters and obtains parameters. springmvc

For more information about setting up the demo environment, see the previous article. This article describes how to submit parameters through url and how to obtain parameters.

[For more information, see http://blog.csdn.net/mahoking]

Common URL submission Parameters

Url: url. do? Param1 = mahc & amp; param2 = 8888.00

You need to add the following method to the HelloController object above:

/*** Spring mvc url submission parameter * @ param name * @ return */@ RequestMapping ("/param") public ModelAndView getInfo (@ RequestParam ("name") String name) {String str = name + "Spring MVC example"; return new ModelAndView ("message", "str", str );}


 

The url format for accessing this method is: param? Name = hoking (Get method ). This is a common submission method. Use the annotation @ RequestParam to bind the request parameter a to variable a. When request parameter a does not exist, an exception occurs. You can set the attribute required = false. For example: @ RequestParam (value = "a", required = false ). In the above text, the submitted parameters are obtained through name.

RESTful URL parameters

Next, let's take a look at the Restful style. The URI of a group of resources, such as http://example.com/resources/single resource, for example, http://example.com/resources/resources/142. For more information, see the following article.

The following describes how to add a HelloController object:

/*** Spring MVC supports RESTful URL parameters ** @ return */@ RequestMapping ("/index/{username }") public String getMessage (@ PathVariable ("username") String username) {System. out. println (username); return "message ";}


 

@ PathVariable is used above. The difference between PathVariable and RequestParam is that.

When @ ing with the @ RequestMapping URI template style, that is, someUrl/{paramId}, the paramId can be bound to the parameter of the method through the @ Pathvariable annotation.

The url format for accessing this method is index/mahoking. @ PathVariable is used to obtain the dynamic parameters in the request url, which is very convenient. Mahoking is the dynamic value of username.

The getMessage () method in the preceding section returns a String object. The value indicates the jump address of the page, excluding the extension (suffix ). In this example, the message. jsp page is displayed.

 

[For more information, see http://blog.csdn.net/mahoking]

 

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.