Spring MVC - Custom Binding with @RequestParam MCQ
0/13
0✓
0✗
1. What is data binding in Spring MVC when it is used with request parameters?
Why: Request parameters arrive as text. Spring MVC binds them by converting the text into the Java type that the handler method needs.
2. Which annotation is used to customize data binding for a controller?
Why:@InitBinder marks a controller method that can customize the data binding process before request values are bound.
3. What is the main purpose of a method annotated with @InitBinder?
Why: An @InitBinder method prepares the WebDataBinder (for example, by registering custom editors) before request data is bound.
4. Which parameter type can an @InitBinder method accept to do the customization?
Why: An @InitBinder method usually takes a WebDataBinder argument. This object is used to register custom editors and other binding settings.
5. What is a PropertyEditor used for in data binding?
Why: A PropertyEditor converts between text and a Java type. It is a common way to customize how request values are bound.
6. Which method of WebDataBinder is used to add a custom PropertyEditor for a specific type?
Why:registerCustomEditor() registers a PropertyEditor for a given type so the binder uses it during conversion.
7. Why would you create a custom binding for a @RequestParam value?
Why: Default conversion may not understand every text format. A custom binding lets you decide exactly how the text is converted, for example a specific date pattern.
8. Spring MVC receives a request parameter as text. What happens before it reaches a handler method argument of type java.util.Date?
Why: Spring MVC converts the request text into the argument type before calling the handler method. Custom binding changes how this conversion works.
9. What does @InitBinder("paramName") do when a name is given in the annotation?
Why: When a name is given, the @InitBinder method applies only to the request parameter or model attribute with that name. Without a name, it applies to all of them in that controller.
10. What is the scope of an @InitBinder method that is declared inside a controller class?
Why: An @InitBinder method inside a controller customizes binding for that controller only.
11. What does CustomDateEditor do?
Why:CustomDateEditor is a ready-made PropertyEditor that parses date text into a java.util.Date using a format you provide.
12. A date editor is created with a date format and an allowEmpty value of true. What does allowEmpty mean?
Why: When empty values are allowed, an empty text is converted to null instead of causing an error.
13. What usually happens if the request text does not match the date format used by the custom editor?
Why: If the text cannot be converted, the binding fails. For a @RequestParam argument, Spring MVC then responds with a 400 (Bad Request) status.
14. Which Java class is commonly used with CustomDateEditor to define the date pattern?
Why:SimpleDateFormat defines the date pattern, such as yyyy-MM-dd, that CustomDateEditor uses to parse the text.
15. Which is a good reason to use @InitBinder instead of writing conversion code inside every handler method?
Why: Putting the conversion rules in an @InitBinder method keeps them in one place. Handler methods then receive ready-to-use Java objects.