R4R Java Spring Spring2.5 MVC Example Example of Validate Interface
previous

Home

Next

Example of Validate Interface

Directory Structure of Spring MVC application:-

index.jsp

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

web.xml

<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<display-name>Spring MVC Application</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>*.bean</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/dispatcher-servlet.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

</web-app>

dispatcher-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">

<bean id="messageSource"
class="org.springframework.context.support.
ResourceBundleMessageSource">
<property name="basename" value="message" />
</bean>
<bean name="/userInfo.bean" class="com.r4r.UserInfoController">
<property name="commandClass" value="com.r4r.UserInfo"/>
<property name="commandName" value="user"/>
<property name="formView" value="formPage"/>
<property name="successView" value="successPage"/>
<property name="validator">
<bean class="com.r4r.MyValidator"/>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.
	InternalResourceViewResolver" >
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
	
</beans>

UserInfoController.java

package com.r4r;

import javax.servlet.http.*;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

public class UserInfoController extends SimpleFormController {
	public UserInfoController() {
		setCommandClass(UserInfo.class);
		setCommandName("user");
	}
protected ModelAndView onSubmit(HttpServletRequest request,
HttpServletResponse response, Object command, BindException errors)
			throws Exception {
	UserInfo userinfo=(UserInfo)command;
	model.addObject("name", userinfo.getName());
	model.addObject("dob", userinfo.getDob());
	model.addObject("address", userinfo.getAddress());
	model.addObject("city", userinfo.getCity());
		return model;
	}
}



UserInfo.java

package com.r4r;

public class UserInfo {
	String name;
	String dob;
	String address;
	String city;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getDob() {
		return dob;
	}
	public void setDob(String dob) {
		this.dob = dob;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
}

MyValidator.java

package com.r4r;

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

public class MyValidator implements Validator {

	@SuppressWarnings("unchecked")
	public boolean supports(Class arg0) {
		
		return UserInfo.class.isAssignableFrom(arg0);
	}

public void validate(Object arg0, Errors errors) {
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name","user.name");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "dob","user.dob");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "address","user.address");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "city","user.city");

	}

}

message.properties

user.name = UserName is required!
user.dob = Date of birth is required!
user.address = Address is required!
user.city = City is required!

formPage.jsp

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
<title>Validation Information Example</title>
<style>
.error {
color: #ff0000;
font-style: italic;
}
.errorblock{
	color: #ff0000;
	background-color: black;
	width:400px;
	text-align: center;
}
</style>
</head>
<body>
<form:form commandName="user" method="POST">
<form:errors path="*" cssClass="errorblock" element="div"/>
<table>
<tr>
<td>UserName:-</td>
<td><form:input path="name"/></td>
<td><form:errors path="name" cssClass="error"/></td>
</tr>
<tr>
<td>Date Of Birth:-</td>
<td><form:input path="dob"/></td>
<td><form:errors path="dob" cssClass="error"/></td>
</tr>
<tr>
<td>Address:-</td>
<td><form:input path="address"/></td>
<td><form:errors path="address" cssClass="error"/></td>
</tr>
<tr>
<td>City:-</td>
<td><form:input path="city"/></td>
<td><form:errors path="city" cssClass="error"/></td>
</tr>
<tr><td><input type="submit" value="Submit"/></td></tr>
</table>
</form:form>
</body>
</html>

successPage.jsp

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<h4>User Information</h4>
<h5>UserName:-<c:out value="${name}"/></h5>
<h5>Date of Birth:-<c:out value="${name}"/></h5>
<h5>Address:-<c:out value="${name}"/></h5>
<h5>City:-<c:out value="${name}"/></h5>

OutPut:-



previous

Home

Next