3:37 pm - Saturday September 21, 0729

Spring MVC Validation Example Prgram Eclipse

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

ProjectStructure:

 

 

 

 

 

 

 

 

 

 

 

 

JarFiles:

 

 

 

 

 

 

 

 

Student.java: 

package com.candidjava;

public class Student {
	private String name;
	private String degree;
	private String mark;
	private String address;
	private String mail;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDegree() {
		return degree;
	}

	public void setDegree(String degree) {
		this.degree = degree;
	}

	public String getMark() {
		return mark;
	}

	public void setMark(String mark) {
		this.mark = mark;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getMail() {
		return mail;
	}

	public void setMail(String mail) {
		this.mail = mail;
	}
}

StudentController.java: 

package com.candidjava;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.candidjava.Student;

public class StudentController extends SimpleFormController {

	public StudentController() {
		setCommandClass(Student.class);
		setCommandName("user");
	}

	protected ModelAndView onSubmit(Object command) throws Exception {
		Student user = (Student) command;

		return new ModelAndView("SuccessPage", "user", user);
	}

}

StudentValidator.java: 

package com.candidjava;

import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

import com.candidjava.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.rejectIfEmptyOrWhitespace(errors, "degree",
				"degree.required");
		ValidationUtils.rejectIfEmpty(errors, "mark", "mark.required");
		ValidationUtils.rejectIfEmpty(errors, "address", "address.required");
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "mail",
				"mail.required");
		Student user = (Student) target;

	}

}

Redirect.jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%
	response.sendRedirect("userRegistration.htm");
%>

FormDetails.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>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Registration Page</title>
<style>
.error {
	color: #ff0000;
	font-style: italic;
}
</style>
</head>
<body bgcolor="pink">

<form:form method="POST" commandName="user">
	<table>
		<tr>
			<td>Enter Ur Name :</td>
			<td><form:input path="name" /></td>
			<td><form:errors path="name" cssClass="error" /></td>
		</tr>
		<tr>
			<td>Enter Degree :</td>
			<td><form:input path="degree" /></td>
			<td><form:errors path="degree" cssClass="error" /></td>
		</tr>
		<tr>
			<td>Enter Mark :</td>
			<td><form:input path="mark" /></td>
			<td><form:errors path="mark" cssClass="error" /></td>
		</tr>
		<tr>
			<td>Enter Address :</td>
			<td><form:input path="address" /></td>
			<td><form:errors path="address" cssClass="error" /></td>
		</tr>
		<tr>
			<td>Enter Mail ID :</td>
			<td><form:input path="mail" /></td>
			<td><form:errors path="mail" cssClass="error" /></td>
		</tr>

		<tr>
			<td colspan="3"><input type="submit" value="Register"></td>
		</tr>
	</table>
</form:form>

</body>
</html>

FormDetails.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 bgcolor="pink">
User Details
<hr>
User Name : ${user.name}
<br />
Degree : ${user.degree}
<br />
Mark : ${user.mark}
<br />
Address : ${user.address}
<br />
Mail : ${user.mail}
<br />

</body>
</html>

Web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<display-name>SpringExample8</display-name>
	<servlet>
		<servlet-name>dispatcher</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>dispatcher</servlet-name>
		<url-pattern>*.htm</url-pattern>
	</servlet-mapping>
	<welcome-file-list>
		<welcome-file>redirect.jsp</welcome-file>
	</welcome-file-list>
</web-app>

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/jspFiles/" p:suffix=".jsp" />

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

	<bean name="/userRegistration.htm" class="com.candidjava.StudentController"
		p:formView="FormDetails" p:successView="SuccessPage" p:validator-ref="StudentValidator" />

</beans>

OUTPUT:    

   

 

 

 

 

 

 

  

 

 

 

  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 DOWNLOAD+SOURCECODE

 

 

 

 

 

   

 

 

 

 

   

 

  

Filed in: Z-A Spring Codes

Comments are closed.