Spring MVC - Binding request/path params to Java Backing Objects MCQ
0/13
0✓
0✗
1. What is the main purpose of the @RequestParam annotation in Spring MVC?
Why:@RequestParam is used to read a single request parameter (e.g., a query parameter or form field) and pass its value to a controller method argument.
2. When should you consider using a JavaBean object instead of multiple @RequestParam annotations?
Why: The tutorial explains that @RequestParam is fine for a few parameters, but as the number of parameters grows, creating a JavaBean to capture them keeps the controller method clean and maintainable.
3. What must a Java class follow so that Spring can bind request parameters to its properties?
Why: Spring data binding relies on the JavaBean convention. The class needs a default constructor and standard getter/setter methods so that Spring can populate its properties from request parameters.
4. By default, what happens if a request parameter marked with @RequestParam is missing from the request?
Why: Method parameters annotated with @RequestParam are required by default. If the parameter is missing, Spring throws an exception unless you set required = false or provide a defaultValue.
5. Which attribute of @RequestParam allows you to make a request parameter optional?
Why: The required attribute controls whether a request parameter is mandatory. Setting required = false makes it optional.
6. What is the effect of supplying a defaultValue on a @RequestParam?
Why: When you specify a defaultValue, Spring uses that value if the request parameter is missing or empty, and it automatically makes the parameter optional (i.e., required becomes false).
7. Which annotation is used to read a value from a URI template variable (for example, /trades/buy/EUR/USD)?
@RequestMapping("{buySell}/{buyCurrency}/{sellCurrency}")
public String handleRequest(/* add annotation here */ String buySell, ...) { ... }
Why:@PathVariable binds a URI template variable to a method argument, whereas @RequestParam binds a query or form parameter.
8. What is the difference between @RequestParam and @PathVariable?
Why: According to the tutorial, @RequestParam is for request parameters (e.g., ?buySell=buy), and @PathVariable is for URI template variables (e.g., /trades/buy/EUR/USD).
9. Which Spring class is primarily responsible for binding request parameters to handler method arguments?
Why: The tutorial states that Spring uses WebDataBinder (a subclass of DataBinder) to bind request parameters to handler method arguments.
10. What does WebDataBinder use to convert request parameter string values to the target property types?
Why:WebDataBinder uses PropertyEditors (from the JavaBeans specification) and/or ConversionService to convert the string values from the request into the correct types for the method arguments or bean properties.
11. When you use a JavaBean to capture request parameters, do you need to annotate each bean property with @RequestParam?
Why: The tutorial notes that when using a binding object, you do not need to use @RequestParam on each argument. Spring matches request parameter names to the bean's property names.
12. Is a binding object automatically added to the model before the handler method is called?
Why: According to the tutorial, when Spring binds request parameters to a JavaBean, the model is already populated with that binding object before the handler method is invoked. You can then access it in the view without explicitly adding it to the model.
13. What happens if you set required = false on a @RequestParam and the parameter is not present in the request?
Why: When required = false and the parameter is missing, Spring does not throw an exception. The argument is set to null (or the equivalent default for the type), and the handler method is executed.
14. Which statement about the value (or name) attribute of @RequestParam is correct?
Why: The value (or name) attribute tells Spring which request parameter to read. For example, @RequestParam("buySell") looks for a parameter named buySell.
15. What is the relationship between WebDataBinder and DataBinder?
Why: The tutorial explicitly states that WebDataBinder is a subclass of DataBinder. It adds web-specific behavior for binding request parameters.