The errors tag

In this section, you will learn about the errors tag of the Spring form tag library.

The errors tag

The errors tag

In this section, you will learn about the errors tag of the Spring form tag library.

This tag provide errors of field in HTML <span> tag. This gives access to the errors developed in your controller or that were developed by any validators of your controller.

Example

Consider the following form :

<form:form>
	<table>
		<tr>
		<td>First Name:</td>
		<td><form:input path="firstName" /></td>
		</tr>
		<tr>
		<td>Last Name:</td>
		<td><form:input path="lastName" /></td>
		</tr>
		<tr>
		<td colspan="2">
		<input type="submit" value="Save Changes" />
		</td>
		</tr>
	</table>
</form:form>

Consider a situation where we want to display all the error messages for the the firstName and lastName fields after form submission. Given below a validator EmployeeValidator for Employee class :

public class EmployeeValidator implements Validator {
	public boolean supports(Class examinee) {
		return Employee.class.isAssignableFrom(examinee);
	}
	
	public void validate(Object obj, Errors errors) {
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "firstName", "required", "Field is required.");
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "lastName", "required", "Field is required.");
	}
}

The form.jsp would be :

<form:form>
	<table>
		<tr>
			<td>First Name:</td>
			<td><form:input path="firstName" /></td>
			<%-- For displaying errors for firstName --%>
			<td><form:errors path="firstName" /></td>
		</tr>
		
		<tr>
			<td>Last Name:</td>
			<td><form:input path="lastName" /></td>
			<%-- For displaying errors for lastName --%>
			<td><form:errors path="lastName" /></td>
		</tr>
		<tr>
			<td colspan="3">
			<input type="submit" value="Submit" />
			</td>
		</tr>
	</table>
</form:form>

When you submit the value without filling the firstName and the lastName or leave blank, the HTML form would look something like below :

<form method="POST">
	<table>
		<tr>
		<td>First Name:</td>
		<td><input name="firstName" type="text" value=""/></td>
		<%-- For displaying errors for firstName --%>
		<td><span name="firstName.errors">Field is required.</span></td>
		</tr>
		
		<tr>
		<td>Last Name:</td>
		<td><input name="lastName" type="text" value=""/></td>
		<%-- For displaying errors for lastName --%>
		<td><span name="lastName.errors">Field is required.</span></td>
		</tr>
		<tr>
		<td colspan="3">
		<input type="submit" value="Submit" />
		</td>
		</tr>
	</table>
</form>

Wildcard functionalities of the errors tag

The wildcard functionalities of the errors tag is given below :

  • path="*" : For displaying all the errors of the of a given page.
  • path="lastName": It will show all the errors associated with the lastName field.
  • If path is omitted: Only object errors are displayed.

Example

Given below an example for showing errors list at the page top and  field related errors are showing next to the fields :

<form:form>
		<form:errors path="*" cssClass="errorBlock" />
		<table>
			<tr>
			<td>First Name:</td>
			<td><form:input path="firstName" /></td>
			<td><form:errors path="firstName" /></td>
			</tr>
			<tr>
			<td>Last Name:</td>
			<td><form:input path="lastName" /></td>
			<td><form:errors path="lastName" /></td>
			</tr>
			<tr>
			<td colspan="3">
			<input type="submit" value="Submit" />
			</td>
			</tr>
		</table>
</form:form>

The HTML of the above page would look like this :

<form method="POST">
	<span name="*.errors" class="errorBlock">Field is required.<br/>Field is required.</span>
	<table>
		<tr>
			<td>First Name:</td>
			<td><input name="firstName" type="text" value=""/></td>
			<td><span name="firstName.errors">Field is required.</span></td>
		</tr>
		
		<tr>
			<td>Last Name:</td>
			<td><input name="lastName" type="text" value=""/></td>
			<td><span name="lastName.errors">Field is required.</span></td>
		</tr>
		<tr>
			<td colspan="3">
			<input type="submit" value="Submit" />
			</td>
		</tr>
	</table>
</form>