Summary of parameter annotations for some spring MVC controllers

Source: Internet
Author: User

Yesterday colleague asked me the controller parameter annotation problem, I have not written like that for a long time, the parameters and URL set together, but today I read some articles, check some information, I try to use my own understanding of the way to explain it!

[email protected] Bind order Request parameter Value

@RequestParam is used to map the request parameter area data to the parameters of a functional processing method.

[Java] View plain copypublic string requestparam1 (@RequestParam string username)  

  

The request contains a username parameter (such as/requestparam1?username=zhang), which is automatically passed in.

Special attention here: Right-click the project, select Properties, open the Properties dialog box, select Java Compiler and then open the tab "Add variable attributes to generated class files" uncheck, This means that the local variable information is not added to the class file, as shown in 6-12:

Figure 6-12

When you enter a URL in the browser, such as "requestparam1?username=123" will report the following error

Name for argument type [java.lang.String] isn't available, and parameter name information not found in class file either, indicating Without the parameter name of the function processing method, we need to enter the parameter as follows:

[Java] View plain copy  Public String Requestparam2 (@RequestParam ("username") string username)  

That is, by @requestparam ("username"), it is explicitly told that spring WEB MVC uses username to enter parameters.

What are the main parameters @RequestParam annotations:

Value: The parameter name, that is, the parameter name of the request parameter, such as username indicates the parameter area of the request name is username The value of the argument will be passed in;

Required: whether must, the default is true, indicates the request must have the corresponding parameter, otherwise will report 404 error code;

defaultvalue: The default value, which indicates that the default value can be a spel expression, such as "#{systemproperties[' java.vm.version '}" if there is no default value for the parameter with the same name in the request.

[Java] View plain copypublic string requestparam4 (@RequestParam (value= "username", Required=false) string username)   

  

Indicates that there can be no parameter with the name username in the request, and if there is no default of NULL, here are a few points to note:

Atomic type: Must have a value, otherwise throws an exception, and if null values are allowed, use the wrapper class instead.

Boolean wrapper type type: Default Boolean.false, other reference types default to NULL.

[Java] View plain copypublic String requestparam5 (  @RequestParam (value= "username", Required=true, defaultvalue= " Zhang ") String username)   

Indicates that if the request does not have a parameter named username, the default value is "Zhang".

If there are multiple requests with the same name, how should they be received? When authorizing a user, multiple permissions may be granted, first look at the following code:

[Java] View plain copypublic string Requestparam7 (@RequestParam (value= "role") string rolelist)  

If the request parameter is similar to Url?role=admin&rule=user, then the actual rolelist parameter data is "Admin,user", that is, the use of "," split between multiple data; We should use the following method to receive multiple request parameters:

Public String requestparam7 (@RequestParam (value= "role") string[] rolelist)     

Or

Public String REQUESTPARAM8 (@RequestParam (value= "list") list<string> list)      

[email protected] Binding URI template variable value

@PathVariable is used to map template variables in the request URL to the parameters of a functional processing method.

1 @RequestMapping (value= "/users/{userid}/topics/{topicid}")  2 Public String Test (  3        int  userId,   4        int

If the requested URL is "controller url/users/123/topics/456", the template variable {userId} and {topicid} in the URL are automatically bound to the parameter with the same name through the @pathvariable annotation, that is, after the parameter is entered userid=123, topicid=456

So, about the controller parameters, first introduced so much.

Summary of parameter annotations for some spring MVC controllers

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.