liveBook · Manning

@RequestParam annotation

As explained in Spring Start Here, Second Edition

The @RequestParam annotation binds a Spring MVC controller method parameter to an HTTP request parameter.

In Spring MVC, @RequestParam binds a controller method parameter to a value in the HTTP request query string, so the controller can use data sent by the client. section 8.1.2

A request parameter is written as a key-value pair after the path:

  • Use ? before the first pair and & between pairs, as in /home?color=blue&name=Jane. section 8.1.2
  • Parameters are required by default; set required=false in @RequestParam to allow a value to be omitted. section 8.1.2
Listing 8.3 Getting a value through a request parameter
@Controller
public class MainController {
 
  @RequestMapping("/home")
  public String home(
      @RequestParam String color, #A
      Model page) {   #B
    page.addAttribute("username", "Katy");
    page.addAttribute("color", color);    #C
    return "home.html";
  }
}

See a controller method that receives color with @RequestParam and passes it to the view through the Model. section 8.1.2

How Spring Start Here, Second Edition covers it

How deep it goes
Explains parameter-name binding, query-string syntax, and required versus optional values.
Examples
Uses color and name query parameters to pass values from a client through a controller to a view.
What you can do after
Apply it via Spring MVC to receive client-supplied query values in a controller method.
How it connects
Connects HTTP request parameters to Spring MVC controller methods and the Model.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring Start Here, Second Edition. The section links point to the parts of the book it draws on.