Accepting Request Input using @RequestParam, @PathVariable and Form Handeling Spring MVC

No comments

In our last SPRING MVC article we explained you how you can create and dynamic Spring MVC web project with MAVEN without using web.xml with java based configuration and also explained you the flow of request through dispatcher servlet to controller and then back to view.

But any web application is not complete if it cannot accept input from the client. So in this article we will show you how you can get the input from the client using Spring MVC

Spring MVC provides three ways in which you can get the input from a by request from a client inside a controller method.

1. By Request Parameters 2. By Path Variables 3. Form Parameters

1. Getting Input from request parameters with @RequestParam

Suppose we are making a GET request which has url like - /home?userId=123
Then in this url userId is the request parameter with value 123. And to capture this value in our @Controller annotated method we use @RequestParame annotated method parameter as shown in the example below.

@RequestMapping(value= "/home", method=RequestMethod.GET)
 public String home (@RequestParam("userId") int id){
  System.out.println(id);
  return "home";
 }

Now if controller gets a request with query parameter with name userId it will be captured by id variable which is of type int.

Using default value for @RequestParam

We can also define a default value for @RequestParam annotated variable using defaultValue as shown in the next example.

@RequestMapping(value= "/home", method=RequestMethod.GET)
 public String home (@RequestParam(value="userId", defaultValue="255") int userId){
  System.out.println(userId);
  return "home";
 }

So if we do not provide any query parameter just simple /home url then userId variable will have a default value 255. We have provided 255 as a String because query parameters are always of type String , but later it will get converted into int because userId variable is an int.

2. Getting Values from request as Path Variable using @PathVariable

Path variables are another way of getting data as path variable, here in the url we do not use key value pair like query parameter to pass the value but the value is sent as the part of the url path.

Like for url - /home/jonney

Here we can get the name jonney as part variable in spring controller using @PathVariable annotated method parameter.

For identifying the variable portion of the Url path we use a variable inside the url of the @RequestMapping annotated url, which is surrounded by curly braces.

For url - /home/jonney , to get the path variable jonney we can use the following controller method.

@RequestMapping(value= "/home/{userName}", method=RequestMethod.GET)
 public String home (@PathVariable("userName") String name){
  System.out.println(name);
  return "home";
 }

In above example name String parameter has been used to capture the path variable which has been passed to the variable part of @RequestMapping url {userName}

3. Accessing The Form Data In Spring MVC

There are two parts of this process, first is to display the form with some object bound to the form and other is getting the values from the form after form is submitted.

To bind the data of form to an object we will send an empty Object say User in model and bind the form with this Object using modelAttribute

Controller method for Displaying the form -

@RequestMapping(value="register-emp", method=RequestMethod.GET)
 public ModelAndView registerEmployeeForm(){
  ModelAndView model = new ModelAndView("register_employee", "user", new Employee());
  return model;
 }

In above method we are sending an Employee class Object with Model as attribute name 'user' and the view name is 'register_employee'

register_employee.jsp
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<div style="text-align:center">
<form:form method="post" modelAttribute="user">
  <table>
  <tr>
   <td>User Name</td>
   <td><form:input path="name" type="text" /> <!-- bind to user.name--></td>
  </tr>
  <tr>
   <td>User Email</td>
   <td><form:input path="email" type="text" /> <!-- bind to user.email--></td>
  </tr>
  <tr>
   <td>User Phone</td>
   <td><form:input path="phone" type="number" /> <!-- bind to user.name--></td>
  </tr>
  <tr>
   <td>User Password</td>
   <td><form:input path="password" type="password" /> <!-- bind to user.name--></td>
  </tr>
  <tr>
   <td><button type="submit">SUBMIT</button> <!-- bind to user.name--></td>
  </tr>
 </table>
 </form:form>
</div>

As you can notice from above form we have not used action attribute because we want to submit the form to the same url.

Controller Method to get the form Values

@RequestMapping(value="register-emp", method=RequestMethod.POST)
 public ModelAndView registerEmployee(@ModelAttribute("user") Employee emp){
  ModelAndView model = new ModelAndView("emp_details");
  model.addObject("user", emp);
  return model;
 }

As you can see we have used @ModelAttribute annotated method parameter, this is used to get the Model attribute with key ‘user’ which is obviously the user object we want to access.
After we get the values we are sending it to another jsp emp_details.jsp with model to show them.

Registration Form Screenshot
After Submitting Form Screenshot

For any questions or query, feel free to leave comments below. Thanks!


No comments :

Post a Comment