1. What is the main purpose of the @InitBinder annotation in Spring MVC?
Why: @InitBinder marks a method that customizes data binding. Data binding is the process of converting request data into Java objects.
2. Which annotation should be added to the method below to customize data binding?
@Controller
public class MyController {
// Add annotation here
public void customize(WebDataBinder binder) {
}
}
Why: A method annotated with @InitBinder is called by Spring MVC to set up data binding customizations.
3. Which kinds of data can be customized by using @InitBinder?
Why: The customization is not limited to request parameters. It also works for URI template variables and backing/command objects.
4. Where do we normally place an @InitBinder method?
Why: @InitBinder methods are written inside the controller whose data binding we want to customize.
5. Which argument type is typically passed to an @InitBinder method to configure binding?
Why: One of the arguments of an @InitBinder method should be WebDataBinder. We use it to set up the binding customizations.
6. What should be the return type of an @InitBinder method?
Why: An @InitBinder method does not return a value. Its return type must be void.
7. Which argument types are not supported by an @InitBinder method?
Why: An @InitBinder method supports the same arguments as handler methods, except command/form objects and their validation result objects.
8. What is the relationship between WebDataBinder and DataBinder?
Why: WebDataBinder is a subclass of DataBinder. It adds features that are useful for web requests.
9. What can we register with a WebDataBinder?
Why: A WebDataBinder can register custom formatters, validators and PropertyEditors to control how data is bound.
10. Which WebDataBinder method is used to register a custom PropertyEditor?
Why: registerCustomEditor() registers a custom PropertyEditor. addValidators() adds validators and addCustomFormatter() adds formatters.
11. What is a PropertyEditor mainly used for in data binding?
Why: Request values arrive as text. A PropertyEditor converts that text into the required Java type, for example String to Date.
12. What is CustomDateEditor used for?
Why: CustomDateEditor is a ready-made PropertyEditor. It parses date text with a date format and gives a Date object.
13. When is an @InitBinder method called if its value element is not specified?
Why: Without a value element, the method is called on each request handled by that controller.
14. What does Spring pass to an @InitBinder method each time it is called?
Why: Each time the method is called, a new WebDataBinder instance is passed to it.
15. What does the value element of @InitBinder do?
Why: The value element holds one or more names of command/form attributes or request parameters. The method applies only to those names.
16. A controller has a method annotated with @InitBinder("user"). When is this method applied?
Why: The value "user" tells Spring to apply this @InitBinder method only to the attribute or request parameter with that name.
17. Can one controller have more than one @InitBinder method?
Why: We can define multiple @InitBinder methods in a controller. Each one can use different names in its value element.