Spring MVC – @RequestParam

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.


 <h2> Spring MVC Web application </h2>
 <a href = "home_page"> Home page | </a>
 <a href = "about_us"> About Us | </a>
 <a href = "general-form"> Query Form | </a>
 <a href= "register-form" > Registration Form </a>
  • Create the Controller class

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


 import javax.servlet.http.HttpServletRequest;
 import org.springframework.stereotype.Controller;
 import org.springframework.ui.Model;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestParam;
 public class MainController {
             public String showRegiterForm() {
                         return "registerform" ;
             public String showRegisterFormData(@RequestParam("FirstName") String fname,
                                     @RequestParam("LastName") String lname, 
                                     @RequestParam("StudentCourse") String course,
                                     Model m) {
                         String result = "Hi! " + fname+" "+ lname ;  
                         String result2 = "Your Course is " + course ;
                         m.addAttribute("message", result);
                         m.addAttribute("message2" ,result2); 
                         return "regisformdata" ;

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.


 <?xml version = "1.0" encoding = "UTF-8"?>
 <web-app xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
             xmlns = "http://xmlns.jcp.org/xml/ns/javaee"
             xsi:schemaLocation = "http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
             id = "WebApp_ID" version = "3.1">
             <absolute-ordering />
             <!-- Spring MVC Configs -->
             <!-- Step 1: Configure Spring MVC Dispatcher Servlet -->
             <!-- Step 2: Set up URL mapping for Spring MVC Dispatcher Servlet -->
  • 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.


 <?xml version = "1.0" encoding = "UTF-8"?>
 <beans xmlns = "http://www.springframework.org/schema/beans"
             xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" 
             xmlns:context = "http://www.springframework.org/schema/context"
             xmlns:mvc = "http://www.springframework.org/schema/mvc"
             xsi:schemaLocation = "  http://www.springframework.org/schema/beans
             <!-- Step 3: Add support for component scanning -->
             <context:component-scan base-package = " com.app.SpringMVC4" />
             <!-- Step 4: Add support for conversion, formatting and validation support -->
             <!-- Step 5: Define Spring MVC view resolver -->
                         <property name = "prefix" value = "/WEB-INF/view/" />
                         <property name = "suffix" value = ".jsp" />
  • Create all View pages

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


 <%@ page language = "java" contentType = "text/html; charset=ISO-8859-1"
     pageEncoding = "ISO-8859-1"%>
 <!DOCTYPE html>
 <meta charset = "ISO-8859-1">
 <form action = "regis-form" method="get" >
 First Name: <input type = "text" name = "FirstName" />
 Last Name: <input type = "text" name = "LastName" />
 Student Course: <input type = "text" name = "StudentCourse" />
 <input type = "submit" value  = "Submit"/>


 <%@ page language = "java" contentType = "text/html; charset=ISO-8859-1"
     pageEncoding = "ISO-8859-1"%>
 <!DOCTYPE html>
 <meta charset = "ISO-8859-1">
 <title>Insert title here</title>
 <h3>Spring MVC Application</h3>
 FIRST NAME: ${param.FirstName}
 LAST NAME: ${param.LastName} 
 COURSE: ${param.StudentCourse}
 MESSAGE1: ${message}
 MESSAGE2: ${message2}


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