n this tutorial, you will learn how to create a login page using Spring 3 MVC.
Spring 3 MVC Login Form
In this tutorial, you will learn how to create a login page using Spring 3 MVC.
In the given below example, a login page is given in which you need to enter following correct information :
- User Name (Admin)
- Password (root)
If you left any field blank it will display message, also it check the minimum and maximum range of size.
We uses two classes for validating size and empty field - javax.validation.constraints.Size & org.hibernate.validator.constraints.NotEmpty respectively. For this, we add two jars hibernate-validator-4.0.2.GA.jar and validation-api-1.0.0.GA.jar.
Given below the Application flow followed by the code used in the application :
Application Flow :
First page :
When you click on "LoginForm" hyperlink :
If fill incorrect information it will redirect you on a error page :
If login is correct, it will show you the following code :
The project hierarchy of the application is given below :
The jar file used in the application is given below :
Codes :
web.xml ( /Spring3Login/WebContent/WEB-INF/web.xml )
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" 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_5.xsd"> <display-name>Spring3Login</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>/forms/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
index.jsp ( /Spring3Login/WebContent/index.jsp )
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Spring 3, MVC Examples</title> </head> <body> <h1>Spring 3, MVC Examples</h1> <ul> <li><a href="forms/loginform.html">Login Form</a></li> </ul> </body> </html>
dispatcher-servlet.xml ( /Spring3Login/WebContent/WEB-INF/dispatcher-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" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd "> <!-- Enable annotation driven controllers, validation etc... --> <mvc:annotation-driven /> <context:component-scan base-package="net.devmanuals.controllers" /> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"> <value>/WEB-INF/views/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean> <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> <property name="basename" value="/WEB-INF/messages" /> </bean> </beans>
LoginController.java ( /Spring3Login/src/net/devmanuals/controllers/LoginController.java )
package net.devmanuals.controllers; import net.devmanuals.form.LoginForm; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.validation.BindingResult; import java.util.Map; import javax.validation.Valid; @Controller @RequestMapping("loginform.html") public class LoginController { @RequestMapping(method = RequestMethod.GET) public String showForm(Map model) { LoginForm loginForm = new LoginForm(); model.put("loginForm", loginForm); return "loginform"; } @RequestMapping(method = RequestMethod.POST) public String processForm(@Valid LoginForm loginForm, BindingResult result, Map model) { String userName = "Admin"; String password = "root"; if (result.hasErrors()) { return "loginform"; } loginForm = (LoginForm) model.get("loginForm"); if (!loginForm.getUserName().equals(userName) || !loginForm.getPassword().equals(password)) { return "loginerror"; } model.put("loginForm", loginForm); return "loginsuccess"; } }
LoginForm.java ( /Spring3Login/src/net/devmanuals/form/LoginForm.java )
package net.devmanuals.form; import javax.validation.constraints.Size; import org.hibernate.validator.constraints.NotEmpty; public class LoginForm { @NotEmpty @Size(min = 1, max = 50) private String userName; @NotEmpty @Size(min = 1, max = 20) private String password; public void setUserName(String userName) { this.userName = userName; } public String getUserName() { return userName; } public void setPassword(String password) { this.password = password; } public String getPassword() { return password; } }
messages.properties ( /Spring3Login/WebContent/WEB-INF/messages.properties )
NotEmpty.loginForm.userName=must not be blank. Size.loginForm.userName=size must between 1 to 50 characters. NotEmpty.loginForm.password=must not be blank. Size.loginForm.password=size must between 1 to 20 characters.
loginform.jsp ( /Spring3Login/WebContent/WEB-INF/views/loginform.jsp )
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Spring3Example</title> </head> <body> <h3>Login Form</h3> <FONT color="blue"> <h5>User Name="Admin" and Password="root"</h5> </FONT> <form:form action="loginform.html" commandName="loginForm"> <table> <tr> <td>User Name:<FONT color="red"><form:errors path="userName" /></FONT></td> </tr> <tr> <td><form:input path="userName" /></td> </tr> <tr> <td>Password:<FONT color="red"><form:errors path="password" /></FONT></td> </tr> <tr> <td><form:password path="password" /></td> </tr> <tr> <td><input type="submit" value="Submit" /></td> </tr> </table> </form:form> </body> </html>
loginsuccess.jsp ( /Spring3Login/WebContent/WEB-INF/views/loginsuccess.jsp )
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="core" uri="http://java.sun.com/jsp/jstl/core"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Spring3Example</title> </head> <body> <h3>Welcome <core:out value="${loginForm.userName}" /></h3> <table> <tr> <td><a href="loginform.html">Back</a></td> </tr> </table> </body> </html>
loginerror.jsp ( /Spring3Login/WebContent/WEB-INF/views/loginerror.jsp )
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Spring3Example</title> </head> <body> <h3>Login Error !!! Click below to login again</h3> <table> <tr> <td><a href="loginform.html">Retry</a></td> </tr> </table> </body> </html>
[ 0 ] Comments