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]