English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Spring MVC RequestParam Annotation

In Spring MVC, @RequestParam Annotations are used to read form data and automatically bind it to the parameters provided by the method. Therefore, it ignores HttpServletRequest The requirements for the data provided by the object reader.

Including form data, it also maps request parameters to query parameters and parts in multipart requests. If the method parameter type is Map and the request parameter name is specified, the request parameter value is converted to Map; otherwise, all request parameter names and values are used to fill the map parameter.

Spring MVC RequestParam example

We create a login page that includes username and password. Here, we use specific values to verify the password.

1Add the dependency to pom.xml

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.1.1.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>  
    <groupId>javax.servlet</groupId>  
    <artifactId>servlet-api</artifactId>  
    <version>3.0-alpha-1</version>  
</dependency>

2Create a request page

This is a login page that receives the username and password from the user. Here, we use specific values to verify the password.

index.jsp

<html>
<body>
<form action="hello">
UserName:  <input type="text" name="name"/>  <br><br> 
Password:  <input type="text" name="pass"/>  <br><br> 
<input type="submit" name="submit">
</form>
</body>
</html>

3Create a controller class

In the controller class:

@RequestParam is used to read the HTML form data provided by the user and bind it to the request parameters. The model includes the request data and provides it to the view page.

HelloController.java

package com.w3codebox;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class HelloController {}}
    @RequestMapping("/hello)
    //Read the provided form data
    public String display(@RequestParam("name") String name, @RequestParam("pass") String pass, Model m)
    {
        if(pass.equals("admin"))
        {
            String msg="Hello"+ name;
            //Add a message to the model
            m.addAttribute("message", msg);
            return "viewpage";
        }
        else
        {
            String msg="Sorry"+ name+". You entered an incorrect password";
            m.addAttribute("message", msg);
            return "errorpage";
        }   
    }
}

4, create other view components

To run this example, the following view components must be located in the WEB-INF/in the jsp directory.

viewpage.jsp

<html>
<body>
${message}
</body>
</html>

errorpage.jsp

<html>
<body>
${message}
<br><br>
<jsp:include page="/index.jsp></jsp:include>
</body>
</html>
</html>

Output: