PROBLEM
Validation is a common part of a web application. As an end-user interacts with an application and submits data, the data needs to be verified that it conforms to pre-defined rules. While validation logic can be co-located with application logic, this limits the re-usability of application logic and pollutes the application logic.
SOLUTION
There are multiple solutions to validation, and this solution is focussed on using Spring Validation. Spring Validation let's you define your validation logic in a java class that implements the Spring Validator interface. The class will contain one or more validation methods, and a particular method will specify the validation logic for the set of data provided by the web client. Any identified validation errors will be reported back to the end-user.
The steps for configuring validation are:
Implement a validation class then extends
org.springframework.validation.Validator
(see example
below)
Configure the URL Mapping to use the validation class and method (Data Validation) that should be used for the URL. Specify the page (Error View) that should be used if validation fails.
Add an input variable of type
org.springframework.validation.BindingResult
to the
operation that was configured for validation (see URL mapping from
step 2)
HOW IT WORKS
When validation is configured using the steps above, Skyway Builder will emit the code into the controller method that calls the validation class. If the validation passes, the remainder of the operation logic will be executed and the view specified in the URL mapping will be rendered. If the validation fails, the error view specified in the URL mapping will be rendered.
The following validation example shows the implementation of a Spring validation class and method.
Example 2.35. Validation Class
package com.pam.web;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;
import com.pam.domain.Appraisal;
public class ValidateAppraisal implements Validator{
public boolean supports(Class clazz) {
return clazz.equals(Appraisal.class);
}
public void validate(Object command, Errors errors) {
Appraisal xyz = (Appraisal) command;
ValidationUtils.rejectIfEmpty(errors, "employeeNo",
"required.employeeNo",
"Employee number must not be empty.");
}
}
This Spring class stores validation errors | |
This Spring helper class provides methods for rejecting empty fields. | |
The class is the model object that incoming data is being bound to. | |
A validator class must implement the Validator interface. To use this validator, you must specify this class name as a binding option in the Bind tab of URL Mapping editor. | |
You can specify one or more validatation methods that perform validation logic. To use this validation method, you must specify this method name as a binding option in the Bind tab of URL Mapping editor. | |
The command object must be cast to the model object. | |
In addition to specifying validation logic through code, you can use some of the helper methods that are available in the ValidationUtils class. |
Once the validation logic has been coded, the validation class can be used for a URL Mapping in the Data Validation tab of a URL Mapping.
The Spring Form tag library provides an errors tag to render the errors that were created in the validator class. The following block shows a partial listing of the edit.jsp page from the PAM tutorial.
Example 2.36. Using errors tag of the Spring Form Tag Library (edit.jsp)
<%@ page language="java" isELIgnored="false" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@taglib uri="http://www.skywaysoftware.com/taglibs/core" prefix="skyway"%> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> <skyway:javascript src="prototype.js"/> <skyway:javascript src="skyway-internal.js"/> <skyway:javascript src="skyway.js"/> </head> <body> <skyway:form action="${pageContext.request.contextPath}/AppraisalEntryController/ValidateAppraisal.action" commandName="appraisal"> Employee No <skyway:input type="text" id="input_RVkXsl" designPath="PAM.AppraisalEntryController.ValidateAppraisal" path="employeeNo"></skyway:input> <form:errors path="employeeNo" cssClass="error"/> ...full listing omitted for brevity <skyway:button type="submit" label="Next" ></skyway:button> </skyway:form> </body> </html>
RELATED RECIPES