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.



2. Create package and Add Spring jar files(Spring capabilities).
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  

Tags: ,

3 Responses to “Spring MVC Example with Multiple Forms and Validations.”

Unknown said...
December 11, 2017 at 9:04 AM This comment has been removed by the author.

Unknown said...
December 11, 2017 at 9:05 AM

hello, do u have and example of implementing postprocesspage with multi page forms please


mmmmm said...
November 5, 2018 at 5:34 AM

I downloaded the "Spring MVC Example with Multiple Forms and Validations." project and installed it on spring sts.
Performing the compilation of the tomcat project goes into error during activation with the following error

"NFORMAZIONI: Server startup in 20912 ms
nov 05, 2018 2:18:13 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
AVVERTENZA: No mapping found for HTTP request with URI [/SpringMvcFormExample/] in DispatcherServlet with name 'SpringDispatcher'"
I think the project is incomplete.
Can you send me the correct code?

I have sent the download from the page:
https://javafws.blogspot.com/2013/04/spring-mvc-example-with-multiple-forms_9.html#comment-form

I'm new to java and spring mvc and the project is what I was looking for to create an application with multi-page form

my mail is: misonsan@gmail.com


Thank you

Moreno


Post a Comment

Thanks for your comments

Subscribe

© 2014 Java Frameworks. All rights reserved.
Designed by Blogger