The errors tag

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

Ads

Tutorials   
Spring 3.2 MVC insert and retrieve blob from the database The hidden tag The errors tag net.roseindia.dao net.roseindia.service net.roseindia.model net.roseindia.controller Spring 3.2 MVC Hibernate Example Spring 3.2 MVC, Upload File in a specific folder Spring 3.2 MVC Form Handling The textarea tag The options tag The option tag The select tag The radiobuttons tag The radiobutton tag The checkboxes tag mvc:default-servlet-handler Static Resources Configuration View Controllers Configuration View resolvers Spring 3.2 MVC Hello World Example Content Negotiation Configuration Interceptors Configuration using Java or XML Customizing the MVC Java config or XML Namespace MVC Java Config or the MVC XML Namespace Servlet container initialization through code Support for ETag The checkbox tag Autogenerated logical view name through RequestToViewNameTranslator Modification in conventional ModelAndView ControllerClassNameHandlerMapping class for handling convention mapping The password tag The input tag The form tag Form Tag library configuration Customizing the Default Error Page @ExceptionHandler & @ResponseStatus annotation Multipart support for file upload in Spring MVC Themes and Theme resolvers in Spring MVC Locales in Spring MVC ContentNegotiatingViewResolver Redirecting and forwarding to views Chaining of Multiple view resolvers View Resolving through ViewResolver interface Requests Intercepting through a HandlerInterceptor Defining handler methods using @RequestMapping annotation Spring 3.2 Asynchronous Request Processing @RequestMapping annotation for mapping requests @Controller annotation for defining new controller
Ads

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>


Advertisements

Ads
Share on Google+Share on Google+

The errors tag

Posted on: March 29, 2013 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: The errors tag  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads