Interview Questions: Spring Validation
June 11, 2013
|Qns-1: How do we do validation in Spring?|
|Ans: Spring uses Validation framework for validation. org.springframework.validation.Validator is an interface and our class implements this interface. Validator interface has two methods supports (Class) and validate (Object, org.springframework.validation.Errors). These two methods must be implemented. Errors object has all the errors found in the validation.|
|Qns-2: How to resolve codes to error messages?|
|Ans: MessageCodesResolver has the method rejectValue(“property name”,”messages”) like rejectValue("name", "emp.name").|
|Qns-3: When do we use BeanWrapper in Spring?|
|Ans: BeanWrapper is used to set and get property values. It supports the setting of indexed properties.|
|Qns-4: Where do we use PropertyEditor in spring?|
Ans: There can be below scenario to use PropertyEditor.
a. PropertyEditor is used to set properties on beans.
b. PropertyEditor is used to parse HTTP request parameters.
|Qns-5: What is the role of PropertyEditorRegistrars ?|
|Ans: In case when we want to use same set of property editors in several different situations|
|Qns-6: What is the role of ConverterFactory in spring validation?|
|Ans: In case we want to centralize the conversion logic, for example converting from String to Enum, we need to implement ConverterFactory.|