Johan
Johan

Reputation: 3819

@RequestParam, name vs value attributes

I am reading the documentation of the @RequestParam annotation in Spring MVC.

What is the difference between name and value attributes?

The documentation says:

value : Alias for name().

name: The name of the request parameter to bind to.

What does it mean Alias for name() ?

Suppose you have:

http://localhost:8080/springmvc/hello/101?param1=10&param2=20

public String getDetails(
    @RequestParam(value="param1", required=true) String param1,
    @RequestParam(value="param2", required=false) String param2){
        ...
}

for example, value="param1" is the name of request-parameter to bind, while String param1 is the object to bind to.

How could I use name attribute here?

Upvotes: 26

Views: 35814

Answers (1)

Monzurul Shimul
Monzurul Shimul

Reputation: 8386

Functionality of both are same with just different alternative naming. Whichever you prefer to use you will get same functionality. Any one can be used but if you used both make sure to use same value for them, otherwise you will get exception.

You are allowed to use like this:

@RequestParam(value="param1", required=true)
@RequestParam(name="param1", required=true)
@RequestParam(value="param1", required=true, name="param1")

But not this:

@RequestParam(value="param1", required=true, name="param3")

Reference: http://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/bind/annotation/RequestParam.html

Upvotes: 38

Related Questions