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
