Keep Traveling

Spring Form Validation Example

Here is one simple example to validate form (Student form). Spring API providers Validator interface to implement form validations. To implement validation we have to create a validator class which implements the Validator interface and should override validate() method. In this example i am going to validate two text field for Student form

1. Create a Validator class

Create a dynamic web project in Eclipse and add all spring mvc related jars in the web/lib folder, see this example for more details (Spring MVC Example). Once we created the web application create a Validator class.

package com.pretech;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;
import com.pretech.Student;
public class StudentValidator implements Validator {
	
	public boolean supports(Class<?> clazz) {
		return Student.class.isAssignableFrom(clazz);
	}
	public void validate(Object target, Errors errors) {
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "name.required");
		ValidationUtils.rejectIfEmpty(errors, "address", "address.required");
		Student student = (Student) target;
		
	}
}

2. Add Validator details into dispatcher-servlet.xml

<bean id="StudentValidator" class="com.pretech.StudentValidator" />

3. Message.properties



Create  message.properties and placed in to src folder with below properties

name.required = Student Name should not be blank
address.required = Student Address should not be blank

Add below Message Resources in despatcher-servlet.xml file

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource" p:basename="messages" />



4. Add Validator reference to the bean which we are going to validate

<bean name="/studentRegistration.htm" class="com.pretech.StudentController"
  p:formView="StudentDetails" p:successView="SuccessPage" p:validator-ref="StudentValidator" />

5. Add form error details in to JSP page to display

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="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>Student Registration Page</title>
<style>
.error {
color: #ff0000;
font-style: italic;
}
</style>
</head>
<body>
<form:form method="POST" commandName="student">
	<table>
		<tr>
			<td>Enter Student Name :</td>
			<td><form:input path="name" /></td>
			<td><form:errors path="name" cssClass="error" /></td>
		</tr>
		<tr>
			<td>Enter Student Address :</td>
			<td><form:input path="address" /></td>
			<td><form:errors path="address" cssClass="error" /></td>
		</tr>
		
		<tr>
			<td colspan="3"><input type="submit" value="Register"></td>
		</tr>
	</table>
</form:form>
</body>
</html>

6. Deploy and run application


To test the validator click on Register button without any values


image


7. Download Example


Spring MVC Form Validation Example

No comments:

Post a Comment

Featured Post

Confusion Matrix + Precision/Recall (Super Simple, With Examples)

  Confusion Matrix + Precision/Recall (Super Simple, With Examples) 1) Binary Classification Setup Binary classification means the model p...

Featured Posts