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¶m2=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?
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.
@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.
@RequestParam annotation enables spring to extract input data that may be passed as a query, form data, or any arbitrary custom data.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With