Thursday, May 19, 2016

Spring MVC Tutorial part IV - How to use RequestParam annotation

@RequestParam

It will be used in query string to bind the parameter with controller method parameters. We can see some possibility way of using request param.

1. Simple Request Param

@RequestMapping(value = "/order/details", method = RequestMethod.GET)
  public String orderDetails(@RequestParam("orderDevice") String device,
                             Model model) {
    return "orderDetails";
  }

In the above method, the URI will be formed like /order/details?orderDevice=Laptop and where Laptop is value of orderDevice argument.

2. Request Param with Required attribute

@RequestMapping(value = "/order/details", method = RequestMethod.GET)
  public String orderDetails(@RequestParam("orderDevice", required=false)
                             String device, Model model) {
    return "orderDetails";
  }  

If the value is missing for the orderDevice parameter, which value will be set to null,  else value will be passed as usual.

Note:
Required attribute default value is True. If the parameter is missing status code 400 will be returned.

3. Request Param with Default value

@RequestMapping(value = "/order/details", method = RequestMethod.GET)
  public String orderDetails(@RequestParam("orderDevice",
                       defaultValue="MobileDevice") String device, Model model) {
    return "orderDetails";
  }

If the value is missing for the orderDevice parameter, which value will be set to MobileDevice as a default value.

4. Request Param with multiple param values

@RequestMapping(value = "/order/details", method = RequestMethod.GET)
  public String orderDetails(@RequestParam("orderDevice") String device, 
                       @RequestParam("deviceId") String deviceId, Model model) {

   model.addAttribute("msg", "Order device and its Id : "+
                                                 device+", "+deviceId);
   return "orderDetails";
  }

Above will be mapped to /order/details?orderDevice=Laptop&deviceId=123

5. Request Param with Map object

@RequestMapping(value = "/order/details", method = RequestMethod.GET)
  public String orderDetails(@RequestParam Map<String, String> queryMap,
                             Model model) {

   String device = queryMap.get("orderDevice");
   int deviceId = queryMap.get("deviceId");

   model.addAttribute("msg", "Order device and its Id : "+
                                                 device+", "+deviceId);
   return "orderDetails";
  }

From the above, both orderDevice and deviceId mapped to Map object with the help of requestParam annotation. So you access with Map object like above code.

URI will be like /order/details?orderDevice=Laptop&deviceId=123

6. Request Param with possibility ambiguous

@Controller
@RequestMapping("/Electronics")
public class ElectronicsController {

@RequestMapping(value = "/order/details", method = RequestMethod.GET)
  public String orderDetails(@RequestParam("orderDevice") String device, 
                              Model model) {

   return "orderDetails";

  }


@RequestMapping(value = "/order/details", method = RequestMethod.GET)
  public String orderDetails(@RequestParam("deviceId") String deviceId, 
                              Model model) {

   return "orderDetails";

  }
}

Above method make Run time exception, since complain about ambiguous mapping. Exception is look like Caused by: java.lang.IllegalStateException: Ambiguous mapping.

No comments:

Post a Comment