Sunday, September 15, 2019

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

  1.     <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->  
  2. <dependency>  
  3.     <groupId>org.springframework</groupId>  
  4.     <artifactId>spring-webmvc</artifactId>  
  5.     <version>5.1.1.RELEASE</version>  
  6. </dependency>  
  7.   
  8. <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->  
  9. <dependency>    
  10.     <groupId>javax.servlet</groupId>    
  11.     <artifactId>servlet-api</artifactId>    
  12.     <version>3.0-alpha-1</version>    
  13. </dependency>  

2. Create the request page

It is the login page that receive name and password from the user.
index.jsp
  1. <html>  
  2. <body>  
  3. <form action="hello">  
  4. UserName : <input type="text" name="name"/> <br><br>   
  5. Password : <input type="text" name="pass"/> <br><br>   
  6. <input type="submit" name="submit">  
  7. </form>  
  8. </body>  
  9. </html>  

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
  1. package com.javatpoint;  
  2. import org.springframework.stereotype.Controller;  
  3. import org.springframework.ui.Model;  
  4. import org.springframework.web.bind.annotation.RequestMapping;  
  5. import org.springframework.web.bind.annotation.RequestParam;  
  6.   
  7. @Controller  
  8. public class HelloController {  
  9.   
  10.     @RequestMapping("/hello")  
  11.     //read the provided form data  
  12.     public String display(@RequestParam("name") String name,@RequestParam("pass") String pass,Model m)  
  13.     {  
  14.         if(pass.equals("admin"))  
  15.         {  
  16.             String msg="Hello "+ name;  
  17.             //add a message to the model  
  18.             m.addAttribute("message", msg);  
  19.             return "viewpage";  
  20.         }  
  21.         else  
  22.         {  
  23.             String msg="Sorry "+ name+". You entered an incorrect password";  
  24.             m.addAttribute("message", msg);  
  25.             return "errorpage";  
  26.         }     
  27.     }  
  28. }  

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
  1. <html>  
  2. <body>  
  3. ${message}  
  4. </body>  
  5. </html>  
errorpage.jsp
  1. <html>  
  2. <body>  
  3. ${message}  
  4. <br><br>  
  5. <jsp:include page="/index.jsp"></jsp:include>  
  6. </body>  
  7. </html>  
  8. </html>  
Output:
Spring MVC RequestParam Annotation
Spring MVC RequestParam Annotation
Spring MVC RequestParam Annotation
Spring MVC RequestParam Annotation
Spring MVC RequestParam Annotation

No comments:

Post a Comment

How to DROP SEQUENCE in Oracle?

  Oracle  DROP SEQUENCE   overview The  DROP SEQUENCE  the statement allows you to remove a sequence from the database. Here is the basic sy...