Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@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?

like image 594
Johan Avatar asked Feb 19 '17 11:02

Johan


People also ask

What is the difference between @RequestParam and @PathVariable annotation?

Difference between @PathVariable and @RequestParam in Spring 1) The @RequestParam is used to extract query parameters while @PathVariable is used to extract data right from the URI.

What is value in @RequestParam?

@RequestParam Attributes It indicates the name of the request parameter to bind to. boolean required. It is used to set whether the parameter is required. String value. It is similar to name elements and can be used as an alias.

What does the @RequestParam annotation do?

@RequestParam annotation enables spring to extract input data that may be passed as a query, form data, or any arbitrary custom data.

What is difference between @RequestParam and @QueryParam?

What is main difference between @RequestParam and @QueryParam in Spring MVC controller? They're functionally the same: they let you bind the value of a named HTTP param to the annotated variable. That being said, the question is very broad, so you'll have to specify more detail if you want a more useful answer.


1 Answers

Functionality of both are same with just diffrent 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

like image 97
Monzurul Shimul Avatar answered Oct 07 '22 12:10

Monzurul Shimul