32. Validating End-User Input on Server using Spring Validator

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:

  1. Implement a validation class then extends org.springframework.validation.Validator (see example below)

  2. 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.

  3. 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;1
import org.springframework.validation.ValidationUtils;2
import org.springframework.validation.Validator;
import com.pam.domain.Appraisal;3

public class ValidateAppraisal implements Validator{4

 public boolean supports(Class clazz) {
   return clazz.equals(Appraisal.class);
 }

 public void validate(Object command, Errors errors)5 {
   Appraisal xyz = (Appraisal) command;6
   ValidationUtils.rejectIfEmpty(errors, "employeeNo",
                                         "required.employeeNo",
                                         "Employee number must not be empty.");7
 }
}
        

1

This Spring class stores validation errors

2

This Spring helper class provides methods for rejecting empty fields.

3

The class is the model object that incoming data is being bound to.

4

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.

5

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.

6

The command object must be cast to the model object.

7

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.

Figure 2.21. Validation

Validation

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" %>1
<!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">2
  Employee No
  <skyway:input type="text" id="input_RVkXsl" designPath="PAM.AppraisalEntryController.ValidateAppraisal"
                path="employeeNo"></skyway:input>
  <form:errors path="employeeNo" cssClass="error"/>3

  ...full listing omitted for brevity

  <skyway:button type="submit" label="Next" ></skyway:button>   
</skyway:form>
</body>
</html>

1

This is the JSP directive to include the Spring Form tag library.

2

This is the URL that will be called when the form is submitted. The URL mapping associated with this URL is configured to invoke a Validation class during data binding.

3

The <form:errors> tag will display any validation errors.

RELATED RECIPES

  1. Implementing Client-side Validation using Javascript