1. What is the main job of a Formatter in Spring MVC?
Why: A Formatter defines how data is shown in the user interface. It also converts the text entered by the user into the correct Java type.
2. Which feature was introduced in Spring 3 as a simple alternative to PropertyEditor classes?
Why: Spring 3 introduced the Formatter SPI. It is a simple and robust alternative to PropertyEditor classes in client environments such as web applications.
3. You need general-purpose type conversion logic, for example converting a java.util.Date to a java.lang.Long. Which SPI should you use?
Why: Use the Converter SPI for general-purpose conversion from one type to another. Use the Formatter SPI when you need to parse and print localized values in a web application.
4. Which statement correctly describes the difference between a Converter and a Formatter?
Why: A Converter is a general type-to-type conversion. A Formatter is made for the UI: it prints values as text and parses text back to Java values.
5. What does the ConversionService provide in Spring?
Why:ConversionService gives one unified type conversion API for both the Converter SPI and the Formatter SPI.
6. Which annotation is used on a controller method to customize data binding for that controller?
Why: A method annotated with @InitBinder can customize data binding. For example, it can register formatters or property editors for a controller.
7. Which object is passed to an @InitBinder method so that you can register a custom formatter?
Why:WebDataBinder is the object used to customize binding. You can use it to add formatters and property editors.
8. Which method of WebDataBinder is used to register a formatter for a specific field name?
@InitBinder
public void customize(WebDataBinder binder) {
binder.____(new DateFormatter("MM-dd-yyyy"), "startDate");
}
Why:addCustomFormatter registers a formatter on the binder. You can give field names so the formatter is used only for those fields.
9. Which built-in Spring formatter is used to format and parse java.util.Date values?
Why:DateFormatter formats and parses dates. You can set a pattern such as MM-dd-yyyy to control how the date looks.
10. Which built-in Spring formatter is used to show a number as money with a currency symbol, such as $?
Why:CurrencyStyleFormatter formats numbers as currency. You can set which currency it should use.
11. What does NumberStyleFormatter do?
Why:NumberStyleFormatter formats numbers using a number pattern. For example, it can add commas as grouping separators.
12. Which method of WebMvcConfigurer is used to register a formatter globally?
Why: Override addFormatters in a configuration class and add your formatters to the FormatterRegistry. They then apply globally.
13. When is it a good idea to register a formatter globally?
Why: Global registration is best when formatting depends on the field data type. For formatting that depends on field names, use @InitBinder.
14. What is an advantage of configuring formatters in an @InitBinder method compared to registering them globally?
Why:@InitBinder works on a per-request basis. You can pick field names and use dynamic information, such as a path variable, to choose the formatting.
15. A formatter is registered for a field. In a JSP page, a plain EL expression like ${object.field} does not show the formatted value. Which Spring tag can you use to show it?
Why: The Spring bind tag gives access to the bound value after the formatter has been applied. A plain EL expression skips the formatting.
16. What does a Formatter do with text that a user types into a form field?
Why: A Formatter works in two directions. It prints a Java value as text for the UI, and it parses user text back into a Java value.