Spring Programing laungage

Spring Projects

Spring Project 1

adplus-dvertising
Example of Validate Interface
Previous Home Next

Directory Structure of Spring MVC application is given below:

step 1:

Create index.jsp file

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

step 2:

Create web.xml file

<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>

step 3:

Create dispatcher-servlet.xml file

<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>

step 4:

Create UserInfoController.java file

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;
}
}

step 5:

Create UserInfo.java file

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;
}
}

step 6:

Create MyValidator.java file

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");
}
}

step 7:

Create message.properties file

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

step 8:

Create formPage.jsp file

<%@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>

step 9:

Create successPage.jsp file

<%@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