top of page

Spring MVC: Use Cases for Annotations @PathVariable And @RequestParam.


In this article we are going to explain various use cases for @PathVariable and @RequestParam Annotations.


Technologies in this article :

  1. Maven 4.0.0

  2. JDK 1.8

  3. Spring 4.2.5.RELEASE.

1. @PathVariable Annotation :


It is a parameter level annotation, defined at method arguments. It is used to pass variables through URL in controller. Syntax is shown in image below :


Steps :

1. Define the path variable in curly braces in @RequestMapping annotation's argument.

2. Now use @PathVariable annotation for getting the value of path variable in controllers method argument as shown above.

For passing more than one path variables in controller follow the below syntax :

But if you have more numbers of path variables than above syntax will became clumsy and less readable. Spring mvc provides a solution for that using Map object.

For passing more number of path variables you can use @PathVariable with Map Object for getting their values as shown below :

Steps :

1. Define the path variables in curly braces in @RequestMapping annotation's argument as shown in above image.

2. Now use @PathVariable annotation with Map object for getting the values of path variables in controllers method argument as shown above.


Using this method if you have 10 or 12 path variables than you don't have to use @PathVariable for 10 or 12 times. In place of this use @PathVariable with Map object one time and spring container will inject all the path variables in Map object automatically.

Note** : For using @PathVariable with Map Object you have to use @EnableWebMvc annotation while configuring spring container (Syntax shown below) or <mvc:annoation-driven /> tag in spring-servlet.xml file. Otherwise this approach will not work.



1. @RequestParam Annotation :


It is a parameter level annotation, defined at method arguments. It is used to pass request parameters through URL and through message body in controller. Syntax is shown in image below :


Steps :

1. use @RequestParam annotation with your method argument.

2. use value attribute of @RequestParam to define request parameter name. (mandatory)

3. use required attribute of @RequestParam to define that request parameter is mandatory to pass or not. (optional)

4. use defaultValue attribute of @RequestParam to define default value for request parameter to be passed if not provide by user. (optional)



But if you have more numbers of request parameters than above syntax will became clumsy and less readable. Spring mvc provides a solution for that using Map object.

For passing more number of request parameters you can use @RequestParam with Map Object for getting their values as shown below :


Note** : This approach will work without @EnableWebMvc annotation or without <mvc:annoation-driven /> tag in spring-servlet.xml file.

Find Source Code @GitHub.

In next post we explained How to Handle an HTML form using @RequestParam annotation and using @ModelAttribute annotation.



For updates of our next post please subscribe below and don't forget to like. In case of any queries write in comment section.


Featured Posts
Recent Posts
Archive
Related Posts
Follow Us
  • Facebook Basic Square
  • Twitter Basic Square
  • Google+ Basic Square
bottom of page