We have already discussed an example of Spring MVC in our previous tutorials. Here, we are going to discuss the @RequestParam annotation. In the last case, we had created a bean class for defining the parameters. But in the following example, we are going to use the @RequestParam annotation for automatic binding of the method parameter with the form data.

@RequestParam – It is used to bind the form data values with the parameters available in the Controller class methods. In other words, it indicates that a method parameter can be mapped with a web request parameter. It is used in the Controller class method.

Example of Spring MVC using @RequestParam

Here, we are going to create an example of Spring MVC using the @RequestParam annotation.

Following are the steps to create an example using @RequestParam:

  • Create a request page

In this step, we are going to create a request page named index.jsp.

index.jsp

  • Create the Controller class

In this step, we are going to create a Controller class named MainController.java, which returns the JSP view pages.

MainController.java

In the above class (MainController), we have used @RequestParam annotation instead of using bean class. It is used for retrieving the form data and mapping it with the parameters available in the method.

Here, we have three parameters available in the form FirstName, LastName, and StudentCourse.

  • Add the entry of Controller in web.xml

In this step, we are going to add the entry of the Controller into web.xml.

web.xml

  • Create another XML file for component scanning

In this step, we are going to create an XML file named spring-servlet.xml, which is used for component scanning.

spring-servlet.xml

  • Create all View pages

In this step, we are going to create all the view pages (JSP pages) required in the application.

registerform.jsp

regisformdata.jsp

Output

Spring MVC – @RequestParam
Spring MVC – @RequestParam 1
Spring MVC – @RequestParam 2
Spring MVC – @RequestParam 3

Pin It on Pinterest

Share This