Java Reference
In-Depth Information
form bean type. However, the most important is the validate method, which contains the
validation logic. For mandatory field validation, Spring provides a helper class with static
methods called ValidationUtils . As shown in Listing 3-30, the Errors object reference, the
field name that has to be validated, and an error code are passed to the rejectIfEmpty
method for validation. If there is a validation failure, the error object is populated with
the error message. This message is picked up from a resource bundle based on the sup-
plied error code.
Listing 3-30.
public class PolicyFormbeanValidator implements Validator {
public boolean supports(Class clazz) {
return PolicyFormBean.class.equals(clazz);
public void validate(Object formBean, Errors errors) {
PolicyFormBean policybean = (PolicyFormBean) formBean;
ValidationUtils.rejectIfEmpty(errors, "firstName", "mandatoryfirstname");
Now that the validator is ready, it must be connected to the controller. This is done by
wiring it in the Spring configuration. Apart from this, the resource bundle locator also
must be configured. This is shown in the modified Spring configuration in Listing 3-31.
Listing 3-31. insurance-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
<bean name="viewResolver"
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
Search WWH ::

Custom Search