Spring Programing laungage

Spring Projects

Spring Project 1

adplus-dvertising
Example of AbstractWizardFormController
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("abstractWizardFormController.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="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>
<bean name="/abstractWizardFormController.bean" 
class="com.r4r.AbstractWizardFormControllerExample">
<property name="commandName" value="user"/>
<property name="commandClass" value="com.r4r.User"/>
<property name="pages">
<list>
<value>pageOne</value>
<value>pageTwo</value>
<value>pageThree</value>
</list>
</property>
</bean>
</beans>

step 4:

Create AbstractWizardFormControllerExample.java file

package com.r4r;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.
 AbstractWizardFormController;
public class AbstractWizardFormControllerExample 
extends AbstractWizardFormController {
@Override
protected Object formBackingObject
(HttpServletRequest request)
throws Exception {
 return new User();
}
@Override
protected ModelAndView processFinish(HttpServletRequest req,
HttpServletResponse res, Object command, BindException ex)
throws Exception {
User user=(User)command;
ModelAndView model=new ModelAndView("resultPage");
model.addObject("user", user);
return model;
}
@Override
protected ModelAndView processCancel(HttpServletRequest request,
HttpServletResponse response, Object command, BindException errors)
throws Exception {
 //where is the cancel page?
return new ModelAndView("pageOne");
}
}

step 5:

Create User.java file

package com.r4r;
public class User {
String name;
String address;
String phone;
String dob;
String gender;
String city;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getDob() {
return dob;
}
public void setDob(String dob) {
this.dob = dob;
}
}

step 6:

Create pageOne.jsp file

<%@taglib uri="http://www.springframework.org/tags/form"
prefix="form" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>AbstractWizardFormController</title>
</head>
<body>
<h3>Page Number: One</h3>
<form:form commandName="user" method="post">
Name:-<input type="text" name="name"><br/>
Address:-<input type="text" name="address"><br/>
<input type="submit" value="Next" name="_target1">
<input type="submit" value="Cancel" name="_cancel">
</form:form>
</body>
</html>

step 7:

Create pageTwo.jsp file

<%@taglib uri="http://www.springframework.org/tags/form"
prefix="form" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>AbstractWizardFormController</title>
</head>
<body>
<h3>Page Number: Two</h3>
<form:form commandName="user" method="post">
Phone No:-<input type="text" name="phone"><br/>
DOB:-<input type="text" name="dob"><br/>
<input type="submit" value="Previous" name="_target0">
<input type="submit" value="Next" name="_target2">
<input type="submit" value="Cancel" name="_cancel">
</form:form>
</body>
</html>

step 8:

Create pageThree.jsp file

<%@taglib uri="http://www.springframework.org/tags/form"
prefix="form" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>AbstractWizardFormController</title>
</head>
<body>
<h3>Page Number: Three</h3>
<form:form commandName="user" method="post">
Gender:-<input type="text" name="gender"><br/>
City:-<input type="text" name="city"><br/>
<input type="submit" value="Previous" name="_target1">
<input type="submit" value="Next" name="_finish">
<input type="submit" value="Cancel" name="_cancel">
</form:form>
</body>

step 9:

Create resultPage.jsp file

<%@taglib uri="http://www.springframework.org/tags/form"
prefix="form" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>AbstractWizardFormController</title>
</head>
<body>
<h3>AbstractWizardFormController Example in Spring MVC</h3>
<H4>
Name:-<c:out value="${user.name}"></c:out><br/>
Address:-<c:out value="${user.address}"></c:out><br/>
Phone:-<c:out value="${user.phone}"></c:out><br/>
DOB:-<c:out value="${user.dob}"></c:out><br/>
Gender:-<c:out value="${user.gender}"></c:out><br/>
City:-<c:out value="${user.city}"></c:out>
</H4>
</body>
</html>
</html>

Output:

Previous Home Next