Home Spring Spring 2.5 MVC User Registration Example

 
 

Share on Google+Share on Google+

Spring 2.5 MVC User Registration Example

Advertisement
User registration example in Sprinv MVC.

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(){
Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: August 27, 2010

Related Tutorials

Discuss: Spring 2.5 MVC User Registration Example  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:21
nagaraju kaveti
September 8, 2012
Resume

Hello
raka
December 26, 2012
it nice

it good job
lakshman
November 5, 2012
request for complete tutorial

this tuturial is incomplete...pls update the full explanation...
Fedup
May 2, 2013
Tutorial

Just seems to end at step (6)!
shan
August 19, 2013
Need Some Help

Could you please post this example to my email id. I need it .Please complete this example with controller and validator class...
anu
March 12, 2014
question

where is controllers
anindita
September 20, 2011
Spring-2.5-MVC-User-Registration-Example

Will u please send me complete code of "Spring-2.5-MVC-User-Registration-Example".
ravi
December 20, 2011
springs

from step6 it is missing please send me the code for registration example
Rachel
February 29, 2012
Thanks

Useful Code
DMCA.com