Validation using Spring's Validator:- In Spring we can Validate the form value or do server side validation using the validator Interface. In the Following Example Code I am using the validator interface to validate the login form.
LoginController.java
package com.manish.controller;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import com.manish.commons.Login;
import com.manish.services.LoginService;
public class LoginController extends SimpleFormController {
private LoginService loginService;
public LoginController() {
setCommandClass(Login.class);
setCommandName("login");
}
public void setLoginService(LoginService loginService) {
this.loginService = loginService;
}
@Override
protected ModelAndView onSubmit(Object command) throws Exception {
Login login = (Login) command;
loginService.add(login);
return new ModelAndView("loginsucess", "login", login);
}
}
LoginValidator.java
package com.manish.validator;
import org.springframework.validation.Errors; import org.springframework.validation.ValidationUtils; import org.springframework.validation.Validator;
import com.manish.commons.Login;
public class LoginValidator implements Validator {
public boolean supports(Class aClass) { return Login.class.equals(aClass); }
public void validate(Object obj, Errors errors) { Login login = (Login) obj;
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "userName","username.required", "Required field");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "userPassword","userpassword.required", "Required field"); }
}
Login.java
package com.manish.commons;
public class Login {
private String userName;
private String userPassword;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
}
LoginService.java
package com.manish.services;
import com.manish.commons.Login;
public interface LoginService {
public void add(Login login);
}
LoginServiceImpl.java
package com.manish.services;
import com.manish.commons.Login;
public class LoginServiceImpl implements LoginService {
public void add(Login login) {
System.out.println("Add Login");
}
}
login.jsp
<%@ 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>
<title>Login Form Validation Demo</title>
<style>
.error {
color: #ff0000;
font-style: italic;
}
</style>
</head>
<body>
<form:form method="POST" commandName="login">
User Name : <form:input path="userName" />
<form:errors path="userName" cssClass="error" />
User Password : <form:password path="userPassword" />
<form:errors path="userPassword" cssClass="error" />
<br><br>
<input type="submit" value="Submit">
</form:form>
</body>
</html>
loginsuccess.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-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>Success Page</title>
</head>
<body>
Login Details
User Name :${login.userName}
<br>
User Password :${login.userPassword}
</body>
</html>
message.properties
username.required = Please Enter User Name
userpassword.required = Please Enter User Password
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="/WEB-INF/jsp/" p:suffix=".jsp" />
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource"
p:basename="messages" />
<bean id="loginValidator" class="com.manish.validator.LoginValidator" />
<bean id="loginService" class="com.manish.services.LoginServiceImpl" />
<bean name="/login.htm" class="com.manish.controller.LoginController"
p:loginService-ref="loginService" p:formView="login" p:successView="loginsucess"
p:validator-ref="loginValidator" />
</beans>
0 Comment(s)