I'm new to Spring MVC myself, so sorry is any of the explanations is wrong 
First, what version of Spring do you use? I think the spring:bind tag has been deprecated ... Anyway, my example will be based on Spring 2.5 and I will use the form tag to bind the fields values.
We have the following Employee object:
Code:
public class Employee {
private String employeeNo;
private String firstName;
private String lastName;
// Getters and setters for those 3 fields
}
We'll ensure that those 3 fields aren't empty when the form is submitted. We then need to write a class that implements the Validator interface - there are other ways to validate the form, but implementing the Validator interface is the easiest.
Code:
public class EmployeeValidator implements Validator {
@Override
public boolean supports(Class aClass) {
return Employee.class.isAssignableFrom(aClass);
}
@Override
public void validate(Object target, Errors errors) {
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "employeeNo", "Employee no can't be blank");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "firstName", "First name can't be blank");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "lastName", "Last name can't be blank");
}
}
You have to overrides two method: supports and validate. Override the supports method to ensure the object you're validating is the employee object (or assignable from it). The validation itself is done on the validate method. It takes two parameters: 'Object target' which is the object we're validating and 'Errors errors' which is an error object to which we'll bound any field error.
Spring provides ValidationUtils class to do basic validations. In this instance, I'm using the 'rejectIfEmptyOrWhitespace()' method to check that the field is not empty. The one i'm using takes 3 parameters: the error object, the field to be validated, the error message to be shown.
If there is any error, then a field error is created and bounded to the field.
Finally, the jsp code:
Code:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<html>
<head>
<title>Employee Form</title>
<style>
.error {
color: #ff0000;
font-weight: bold;
}
</style>
</head>
<body>
<form:form method="POST" commandName="employee">
<form:errors path="*" cssClass="error" />
<table>
<tr>
<td>Employee No</td>
<td><form:input path="employeeNo" /></td>
<td><form:errors path="employeeNo" cssClass="error" /></td>
</tr>
<tr>
<td>First Name</td>
<td><form:input path="firstName" /></td>
<td><form:errors path="firstName" cssClass="error" /></td>
</tr>
<tr>
<td>Last Name</td>
<td><form:input path="lastName" /></td>
<td><form:errors path="lastName" cssClass="error" /></td>
</tr>
<tr>
<td><input type="submit" value="SAVE" /></td>
</tr>
</table>
</form:form>
</body>
</html>
I put the error information in two places: at the top of the form which shows all errors thrown.
Code:
<form:errors path="*" cssClass="error" />
Also, below each field by including this code after each field declaration:
Code:
<td>Last Name</td>
<td><form:input path="lastName" /></td>
<td><form:errors path="lastName" cssClass="error" /></td>
Hope it helps