RequestParam annotation in Spring

The RequestParam annotation is used when we want to read web request parameters in our controller class. In other words, the front end is sending us some parameters (from a filled form for example) with keys.

java-featured-image

Use case

Suppose we had a form which purpose was to add an employee to the database. Each employee would have:

  1. ID
  2. First Name
  3. Last Name

Therefore, our form would look like this (assuming that the ID is not auto-incremented):

Employee Form Example

Employee Form Example

Imagine the ugly-looking form above was filled with the following information:

Id=1

First Name=Joe

Last Name=Doe

Now imagine we had the following code:

@Controller
@RequestMapping("/employee/*")
public class EmployeeController {
   @GetMapping("/fill")
   public String fill() {
      return "employeeForm";
   }

   @PostMapping("/fill/process")
   public String processForm(ModelMap model, @RequestParam("id") int id, @RequestParam("First Name") String firstName, @RequestParam("Last Name") String lastName) {
      model.addAttribute("id", id);
      model.addAttribute("firstName", firstName);
      model.addAttribute("lastName", lastName);
      return "index";
   }
}

We just binded web request parameters to method parameters (id, firstName, lastName). For this example to work however, we need to have received the corresponding keys from the frontend, otherwise they will be null.

In the example code above, we are providing value for the request params, for example @RequestParam(“id”). However that value can be omitted if the target variable name is the same as the param name. So we could have the above snippet work like this:

@Controller
@RequestMapping("/employee/*")
public class EmployeeController {
   @GetMapping("/fill")
   public String fill() {
      return "employeeForm";
   }

   @PostMapping("/fill/process")
   public String processForm(ModelMap model, @RequestParam int id, @RequestParam("First Name") String firstName, @RequestParam("Last Name") String lastName) {
      model.addAttribute("id", id);
      model.addAttribute("firstName", firstName);
      model.addAttribute("lastName", lastName);
      return "index";
   }
}

and it works because “id” is the same as the name we are giving it as a method parameter, which is indeed is “id”. In addition, we cannot do the same for the other parameters as “First Name” is not the same as “firstName”.



Required element of @RequestParam

There is also the ‘required’ element that RequestParam supports which does pretty much what it says – it specifies whether a specific param is needed or not. For our example, we could say that the first name is not required. By default, requried will be set to true.

@Controller
@RequestMapping("/employee/*")
public class EmployeeController {
   @GetMapping("/fill")
   public String fill() {
      return "employeeForm";
   }

   @PostMapping("/fill/process")
   public String processForm(ModelMap model, @RequestParam int id, @RequestParam(value = "First Name", requried=false) String firstName, @RequestParam("Last Name") String lastName) {
      model.addAttribute("id", id);
      model.addAttribute("lastName", lastName);
      return "index";
   }
}

Now the firstName is not a requried parameter, hence we are not adding it to our model/map.

@RequestParam also has the ‘defaultValue’ element

If we need a value to be filled in our form but we do not really care what that value is, we could set it to a default value so if the user has not filled it, it will just contain whatever we have set to it. See the following code snippet as a reference:

@Controller
@RequestMapping("/employee/*")
public class EmployeeController {
   @GetMapping("/fill")
   public String fill() {
      return "employeeForm";
   }

   @PostMapping("/fill/process")
   public String processForm(ModelMap model, @RequestParam int id, @RequestParam(value = "First Name", requried=false) String firstName, @RequestParam(value = "Last Name", defaultValue="Doe") String lastName) {
      model.addAttribute("id", id);
      model.addAttribute("lastName", lastName);
      return "index";
   }
}

Now, even if the form was not completely filled (the last name field was omitted by the user), if we referenced the lastName parameter, we will have “Doe” stored in it. A famous example of why we would use the ‘defaultValue’ element would be the date. Suppose we have the same form as above but we also had a “Date” field there. Well if the user/employee did not put anything in that field, we could assume it would be today so we would put today’s date as a default value in there. This is just one example of many.

 

5 1 vote
Article Rating
guest
0 Comments
Inline Feedbacks
View all comments