Spring 2.5 MVC User Registration Example

User registration example in Sprinv MVC.

Spring 2.5 MVC User Registration Example

Spring MVC User Registration example

     

Spring 2.5 MVC User Registration Example

This tutorial shows you how to create user registration application in Spring MVC. This application is not backed with the database. But you can easily modify the Controller class and add database capability and then save the user information into database. This is the next step towards learning Spring MVC. After completing this tutorial you will be able to develop form based applications in MVC. This tutorial will also show you how to validate the user input and display the errors messages on the screen.

In this tutorial, we will create a user registration web application that accept the detail of the use, validate it and then get those values in Controller class. This application can be extends to add the database capability in the Controller class.

Step 1:

Now we will create a index.jsp in project's WebContent folder. In the index.jsp file we will create a hyperlink "New User Registration" that will be linked "registration.html" file. The code of the index.jsp is:

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<html>

<head>

<title>Registration Link Page</title>

</head>

<body bgcolor="#EEEEEE">

<center>

<a href="registration.html">New User Registration</a>

</center>

</body>

</html>

Step 2:

Now we will configured the web.xml file for the DispatcherServlet. The code for web.xml is:

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns="http://java.sun.com/xml/ns/j2ee"

xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd"

version="2.5">

<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>*.html</url-pattern>

</servlet-mapping>

<welcome-file-list>

<welcome-file>index.jsp</welcome-file>

</welcome-file-list>

<taglib>

<taglib-uri>/spring</taglib-uri>

<taglib-location>/WEB-INF/spring.tld</taglib-location>

</taglib>

</web-app>

Step 3:

Now we will create dispatcher-servlet.xml inside the WEB-INF folder that will have all the configuration beans for handle the user registration requests. We will set all the properties for the registration like validator, commandClass, commandName, session, formView, successView etc. You can see how to set these properties in the dispatcher-servlet.xml file. The code of the dispatcher-servlet.xml is:

<?xml version="1.0" encoding="UTF-8"?>

<beans

xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"

xmlns:p="http://www.springframework.org/schema/p">

<bean id="viewResolver"

class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<property name="prefix">

<value>/WEB-INF/jsp/</value>

</property>

<property name="suffix">

<value>.jsp</value>

</property>

</bean>

<bean id="urlMapping"

class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">

<property name="interceptors">

<list>

<ref local="localeChangeInterceptor"/>

</list>

</property>

<property name="urlMap">

<map>

<entry key="/registration.html">

<ref bean="registrationController"/>

</entry>

</map>

</property>

</bean>

<bean id="registrationValidator" class="net.roseindia.web.RegistrationValidator"/>

<bean id="registrationController" class="net.roseindia.web.RegistrationFormController">

<property name="sessionForm"><value>false</value></property>

<property name="commandName"><value>user</value></property>

<property name="commandClass"><value>net.roseindia.web.User</value></property>

<property name="validator"><ref bean="registrationValidator"/></property>

<property name="formView"><value>registration</value></property>

<property name="successView"><value>registrationsuccess</value></property>

</bean>

<bean id="localeChangeInterceptor"
class
="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">

<property name="paramName" value="hl"/>

</bean>

<bean id="localeResolver" class="org.springframework.web.servlet.i18n.Session
LocaleResolver"
/>

</beans>

 

In the above code we have used the form validator with the help of code: <property name="validator"><ref bean="registrationValidator"/></property>

The registrationValidator bean id define in the configuration file using following code:

<bean id="registrationValidator" class="net.roseindia.web.RegistrationValidator"/>

In the net.roseindia.web.RegistrationValidator class we have defined the validation logic.

In this tutorial, dispatcher-servlet.xml provide us how to use viewResolver in the web application. we have added some code for viewResolver which enable you to render models in a browser without tying you to a specific view technology. The code is:

<bean id="viewResolver"

class="org.springframework.web.servlet.view.InternalResourceViewResolver">

   <property name="prefix">

   <value>/WEB-INF/jsp/</value>

  </property>

  <property name="suffix">

   <value>.jsp</value>

   </property>

</bean>

The dispatcher-servlet.xml also configure urlMapping  for a particular request. We have include following code in dispatcher-servlet.xml file for the urlMapping and set the registrationController for the user registration request.

<bean id="urlMapping"

class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">

   <property name="interceptors">

   <list>

   <ref local="localeChangeInterceptor"/>

  </list>

   </property>

   <property name="urlMap">

   <map>

  <entry key="/registration.html">

  <ref bean="registrationController"/>

   </entry>

  </map>

   </property>

</bean>  

 Step 4:

Now we will create registration.jsp inside /WEB-INF/jsp/ folder. In this file we will created user interface for enter registration information by the user. The code of the registration.jsp is:

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

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

<%@ taglib prefix="spring" uri="/spring"%>

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>

<html>

<head><title>User Registration</title></head>

<body>

<center>

<h3>Registration page</h3>

<br/>

<form:form commandName="user" method="POST" name="user">

<table border="0">

<tr>

<td>Username:</td>

<td><form:input path="username"/></td>

<td><font color="red"><form:errors path="username"/></font></td>

</tr>

<tr>

<td>Password:</td>

<td><form:password path="password"/></td>

<td><font color="red"><form:errors path="password"/></font></td>

</tr>

<tr>

<td>Confirm Password:</td>

<td><form:password path="confirmpassword"/></td>

<td><font color="red"><form:errors path="confirmpassword"/></font></td>

</tr>

<tr>

<td>Email ID:</td>

<td><form:input path="emailid"/></td>

<td><font color="red"><form:errors path="emailid"/></font></td>

</tr>

<tr>

<td>Address:</td>

<td><form:input path="address"/></td>

<td><font color="red"><form:errors path="address"/></font></td>

</tr>

<tr>

<td colspan="3" align="center"><input type="submit" value="Save"/></td>

</tr>

</table>

</form:form>

</center>

</body>

</html>

 Step 5:

Now we will create a registrationsuccess.jsp that will display when request process successfully then request controller send response for the user. The registration.jsp display all information of the user that will registered currently. The code of the registration.jsp is:

<%@ page session="false"%>

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

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

<%@ taglib prefix="spring" uri="/spring" %>

<html>

<head>

<title>Success</title>

</head>

<body>

<center>

<h3>User Registered Successfully.</h3><br>

<table>

<tr>

<td colspan="2" align="center"><font size="5">User Information</font></td>

</tr>

<tr>

<td>Username:</td>

<td><core:out value="${user.username}"/></td>

</tr>

<tr>

<td>Email ID:</td>

<td><core:out value="${user.emailid}"/></td>

</tr>

<tr>

<td>Address:</td>

<td><core:out value="${user.address}"/></td>

</tr>

</table>

<a href="registration.html">Back</a>

</center>

</body>

</html>

Step 6:

Now we will created business class in the project src folder for user registration that's name User.java that will have user information related private variable and setter, getter methods for access of these variables. This class is a command class for the user registration request. The code for the User.java is:

package net.roseindia.web;

public class User {
  public User(){

Tutorials

  1. How much time will it take to learn Spring and Hibernate?
  2. Advantages of Spring Framework
  3. Spring 4 Introduction and example
  4. Features of Spring Framework 5
  5. How to make Spring web Login form?
  6. How to make Simple form in Spring web?
  7. Spring, Hibernate login and registration application
  8. Spring Framework Tutorial for beginners with examples
  9. Spring Framework for Apache Hadoop 2.3.0 GA released
  10. Spring Framework 4.1 GA is released with major features
  11. Why to use Spring Framework?
  12. Spring Framework 4.1 - First Release candidate available
  13. Spring IO Platform 1.0.0 Released
  14. Spring 4: Login Form using Spring MVC and Hibernate Example
  15. Spring 4 MVC Login form Example with source code
  16. Spring 4 MVC Hello World Example: Spring 4 MVC Tutorial will full source code
  17. Spring Web MVC Application Error:ClassNotFoundException: DispatcherServlet on deploying
  18. Features of Spring 4
  19. Spring Framework 4.0 released
  20. Spring Framework 4: Spring Framework 4 Tutorials and Example
  21. Spring Integration 3.0 Release is released and available for download
  22. Spring Tutorial for Beginners
  23. Java Springs Framework Tutorial
  24. Spring Architecture
  25. Spring Framework Tutorials
  26. database spring registration form
  27. Spring Login Example
  28. Roseindia Spring Tutorial
  29. Spring Tutorial
  30. Spring 3.2 MVC insert and retrieve blob from the database
  31. The hidden tag
  32. The errors tag
  33. net.roseindia.dao
  34. net.roseindia.service
  35. net.roseindia.model
  36. net.roseindia.controller
  37. Spring 3.2 MVC Hibernate Example
  38. Spring 3.2 MVC, Upload File in a specific folder
  39. Spring 3.2 MVC Form Handling
  40. The textarea tag