Spring MVC Example with Multiple Forms and Validations.
Spring MVC Example with Multiple Forms by using AbstractWizardFormController.
Steps....
1. Create project by using IDE(any) - i am using MyEclipse.
3. Configure deployment descriptor web.xml with following details.
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfingLocation</param-name>
<param-value>WEB-INF/applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>
4. Create another configuration file with servlet-name-servlet(spring-servlet)
5. create folder pages and add following beans to spring-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-3.1.xsd">
<bean id="jspView"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
6. Develop Multiple FormView and place inside the pages folder.
1.AddUser.jsp, 2. AddUserCredential.jsp, 3. AddContactDetails.jsp.
1.AddUser.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<link href="${pageContext.request.contextPath}/css/errors.css" rel="stylesheet" type="text/css">
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<form:form method="post" action="AddUser.html" name="user" commandName="user">
<table>
<tr>
<td><form:label path="name">Name</form:label></td>
<td><form:input path="name" /></td>
<td><form:errors path="name" cssClass="errors"/></td>
</tr>
<tr>
<td><form:label path="username">User Name</form:label></td>
<td><form:input path="username" /></td>
<td><form:errors path="username" cssClass="errors"/></td>
</tr>
<tr>
<td><input type="hidden" name="_page0" value="true" /></td>
<td><input type="hidden" name="_target1" value="true" /><td>
</tr>
<tr>
<td colspan="4">
<input type="submit" value="Next"/>
</td>
</tr>
</table>
</form:form>
2.AddUserCredential.jsp.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<link href="${pageContext.request.contextPath}/css/errors.css" rel="stylesheet" type="text/css">
<%@ 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">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<form:form method="post" action="AddUser.html" name="user" commandName="user">
<table>
<tr>
<td><form:label path="password">Password</form:label></td>
<td><form:password path="password" /></td>
<td><form:errors path="password" cssClass="errors"/></td>
</tr>
<tr>
<td><form:label path="rpassword">Retype Password</form:label></td>
<td><form:password path="rpassword" /></td>
<td><form:errors path="rpassword" cssClass="errors"/></td>
</tr>
<tr>
<td><form:label path="dob">Date of Birth(DD/MM/YY)</form:label></td>
<td><form:input path="dob" /></td>
<td><form:errors path="dob" cssClass="errors"/></td>
</tr>
<tr>
<td><input type="hidden" name="_page1" value="true" /></td>
<td><input type="hidden" name="_target2" value="true" /><td>
</tr>
<tr>
<td colspan="4">
<input type="submit" value="Next"/>
</td>
</tr>
</table>
</form:form>
3.AddContactDetails.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" %>
<link href="${pageContext.request.contextPath}/css/errors.css" rel="stylesheet" type="text/css">
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<form:form method="post" action="AddUser.html" name="user" commandName="user">
<table>
<tr>
<td><form:label path="mobile">Mobile No</form:label></td>
<td><form:input path="mobile" /></td>
<td><form:errors path="mobile" cssClass="errors"/></td>
</tr>
<tr>
<td><form:label path="email">Email</form:label></td>
<td><form:input path="email" /></td>
<td><form:errors path="email" cssClass="errors"/></td>
</tr>
<tr>
<td><input type="hidden" name="_page2" value="true" /></td>
<td><input type="hidden" name="_finish" value="true" /><td>
</tr>
<tr>
<td colspan="4">
<input type="submit" value="Store"/>
</td>
</tr>
</table>
</form:form>
7. Develop POJO class to above Forms User.java
package com.javafws.blog.model;
public class User implements java.io.Serializable {
private String name;
private String username;
private String password;
private String rpassword;
private String mobile;
private String dob;
private String email;
// Generate Getters and Setters.
}
8. Now develop controller to perform the action to the request entered by Client.
AddUserController .java
package com.javafws.blog.controllers;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractWizardFormController;
import com.javafws.blog.model.User;
@SuppressWarnings("deprecation")
public class AddUserController extends AbstractWizardFormController {
public AddUserController() {
setCommandClass(User.class);
setCommandName("user");
}
@Override
protected void validatePage(Object command, Errors errors, int page) {
User user = (User) command;
switch (page) {
case 0:
if(user.getName().equals("")) {
errors.rejectValue("name", "name.required");
}
if(user.getUsername().equals("")) {
errors.rejectValue("username", "username.required");
}
break;
case 1:
if(user.getPassword().equals("")) {
errors.rejectValue("password", "password.required");
}
if(!(user.getRpassword().equals(user.getPassword()))) {
errors.rejectValue("rpassword", "rpassword.required");
}
if(user.getDob().equals("")) {
errors.rejectValue("dob", "dob.required");
}
break;
case 2:
if(user.getMobile().equals("")) {
errors.rejectValue("mobile", "mobile.required");
}
if(user.getEmail().equals("")) {
errors.rejectValue("email", "email.required");
}
break;
}
}
@Override
protected ModelAndView processFinish(HttpServletRequest request,
HttpServletResponse response, Object command, BindException errors)
throws Exception {
User user = (User) command;
ModelAndView mav = new ModelAndView("UserAddedSuccess","user",user);
return mav;
}
}
9. Configure controller in spring-servlet.xml file in the following. And also config messageSource
bean to configure properties file. And place errors.properties file in src folder
<bean name="/AddUser.html"
class="com.javafws.blog.controllers.AddUserController">
<property name="pages">
<value>AddUser,AddUserCredential,AddContactDetails</value>
</property>
</bean>
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="errors" />
</bean>
10. Create errors_en_US.properties properties file.
name.required = Name required.
username.required = Username required.
password.required = Password required.
rpassword.required = Retype Password required.
mobile.required = Mobile No required.
dob.required = Date of Birth required.
email.required = Email required.
11. Dovelope SuccessView available in UserAddedSuccess.java
( ModelAndView mav = new ModelAndView("UserAddedSuccess","user",user); )
Hello ${user.name}
<br>
Your Entered Details are...
<br>
<table>
<tr>
<td>Name :</td>
<td>${user.name}</td>
</tr>
<tr>
<td>User Name :</td>
<td>${user.username}</td>
</tr>
<tr>
<td>Mobile No :</td>
<td>${user.mobile}</td>
</tr>
<tr>
<td>Date of Birth(DD/MM/YY) :</td>
<td>${user.dob}</td>
</tr>
<tr>
<td>Email :</td>
<td>${user.email}</td>
</tr>
</table>
<a href="AddUser.html">Add Another User</a>
Finally URL is http://localhost:8090/SprMVCExample1/AddUser.html
Download Source Code : Download
Get Updates
Subscribe to our e-mail to receive updates.
Related Articles
Subscribe to:
Post Comments (Atom)
2 Responses to “Spring MVC Example with Multiple Forms and Validations.”
December 11, 2017 at 9:04 AM This comment has been removed by the author.
December 11, 2017 at 9:05 AM
hello, do u have and example of implementing postprocesspage with multi page forms please
Post a Comment
Thanks for your comments