Spring MVC RequestParam Annotation
In Spring MVC, the @RequestParam annotation is used to read the form data and bind it automatically to the parameter present in the provided method. So, it ignores the requirement of HttpServletRequest object to read the provided data.
Including form data, it also maps the request parameter to query parameter and parts in multipart requests. If the method parameter type is Map and a request parameter name is specified, then the request parameter value is converted to a Map else the map parameter is populated with all request parameter names and values.
Spring MVC RequestParam Example
Let’s create a login page that contains a username and password. Here, we validate the password with a specific value.
1. Add dependencies to pom.xml
2. Create the request page
It is the login page that receive name and password from the user.
index.jsp
3. Create the Controller Class
In controller class:
- The @RequestParam is used to read the HTML form data provided by a user and bind it to the request parameter.
- The Model contains the request data and provides it to view page.
HelloController.java
4. Create the other view components
To run this example, the following view components must be located inside the WEB-INF/jsp directory.
viewpage.jsp
errorpage.jsp
Output: