In this example you will see how to validate the user registration form that we created in the previous previous example (Spring-Simple Form Controller) To validate the form fields all you need to do is to have a separate UserValidator class that implements the Validator interface, override the validate() method perform all the validations. In the jsp page add form:errors tag to dispaly errors and finally you need to link the UserValidator class with the UserController class. Since the UserControllerclass extends the SimpleFormController class the validate() method will be called automatically. In case you would like to have the error messages in a separate properties file then you need to create a new properties file, add all the error keys and its corresponding values and finally link it to Spring configuration file.
userValidator.java
package com;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;
public class UserValidator implements Validator {
public boolean supports(Class arg0) {
return User.class.isAssignableFrom(arg0);
}
public void validate(Object target, Errors errors) {
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "name.required");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password", "password.required");
ValidationUtils.rejectIfEmpty(errors, "gender", "gender.required");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "aboutYou", "aboutYou.required");
User user = (User) target;
}
}
Here you need to override the validate method. To check mandatory condition you can use the ValidationUtils class methods like rejectIfEmptyOrWhitespace or rejectIfEmpty. These methods takes three arguments the Errors object, the property name, and the error code. Here we have the error messages in a seperate properties file so we add the error code, you can even add the error messages directly. To do any other validation you have access to the domain object, using the domain object validate the properties, incase there are errors you can use the rejectValue() method of the Errors class to add errors. Here the first argument is the property name and the second argument is the error code.
dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/" p:suffix=".jsp" />
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource"
p:basename="messages" />
<bean id="userValidator" class="com.UserValidator"/>
<bean name="/userRegistration.htm" class="com.UserController"
p:formView="userForm" p:successView="userSuccess"
p:validator-ref="userValidator" />
</beans>
messages.properties
In the dispatcher-servlet.xml, we have declared the base name for the ResourceBundleMessageResource as messages, so the property file name should be messages.properties.
The messages.properties file should be placed in the root of the classpath.
name.required=User name is required
password.required=Password is required
gender.required=Gender is required
aboutYou.required=About you is required
userForm.jsp
Now add the form:errors tags in the jsp page to display the errors.
<%@ 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>Registration Page</title>
<style>
.error{
font-style:italic;
color:#ff0000;
}
</style>
</head>
<body>
<form:form method="POST" commandName="user">
<table>
<tr>
<td>User Name :</td>
<td><form:input path="name" /></td>
<td><form:errors path="name" cssClass="error"/></td>
</tr>
<tr>
<td>Password :</td>
<td><form:password path="password" /></td>
<td><form:errors path="password" cssClass="error"/></td>
</tr>
<tr>
<td>Gender :</td>
<td>Male : <form:radiobutton path="gender" value="M" />
Female : <form:radiobutton path="gender" value="F" /></td>
<td><form:errors path="gender" cssClass="error"/></td>
</tr>
<tr>
<td>About you :</td>
<td><form:textarea path="aboutYou" /></td>
<td><form:errors path="aboutYou" cssClass="error"/></td>
</tr>
<tr>
<td colspan="2"><input type="submit"></td>
</tr>
</table>
</form:form>
</body>
</html>
The path attribute of the form:errors tag indicate the property for which the error should be displayed. Use path="*" to display all the errors together
<form:form method="POST" commandName="user">
<form:errors path="*" cssClass="error"></form:errors>
<table>
<tr>
<td>User Name :</td>
<td><form:input path="name" /></td>
</tr>
<tr>
<td>Password :</td>
<td><form:password path="password" /></td>
</tr>
<tr>
<td>Gender :</td>
<td>Male : <form:radiobutton path="gender" value="M" />
Female : <form:radiobutton path="gender" value="F" /></td>
</tr>
<tr>
<td>About you :</td>
<td><form:textarea path="aboutYou" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit"></td>
</tr>
</table>
</form:form>
Don't work, p.basename not bound, Java really sucks omg, 3 hours and i don't found any solution, all tutorial is wrong omg
ReplyDeletewhere and how to set commandName and commandClass ?
ReplyDelete