In this article we will learn how to create a login page using Spring MVC. The user enters the login id and password and we will have a check on the controller to see if the credentials are valid. For this article we will not connect to the database for validation. we will have a separate article on database connectivity for spring mvc.
Please go through the steps to create a simple mvc example if you have not created any spring projects yet.
Creating a Spring MVC Simple Example
Login Model Class
We need to create a Model class that will store the username and password from the login page.
This model class will be passed to controller.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package com.kscodes.sampleproject.model; public class Login { private String userName; private String password; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } } |
Login Controller
Login Controller has 2 methods
1 |
getLoginPage() |
returns the name of the jsp page to displayed along with an empty Login bean.
1 |
processLogin() |
handles the checks if correct username and password were passed. The parameter to this method is a Login bean which contains the userName and password that were entered on the login form.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
package com.kscodes.sampleproject.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; import com.kscodes.sampleproject.model.Login; @Controller public class LoginController { @RequestMapping("/login") public ModelAndView getLoginPage() { return new ModelAndView("login", "command", new Login()); } @RequestMapping(value = "/processLogin", method = RequestMethod.POST) public ModelAndView processLogin(@ModelAttribute("login") Login login) { String userName = login.getUserName(); String password = login.getPassword(); if ("admin".equalsIgnoreCase(userName) && "admin123".equalsIgnoreCase(password)) { ModelAndView mv = new ModelAndView("success"); return mv; } else { ModelAndView mv = new ModelAndView("error"); return mv; } } } |
Login, Success and Error Pages
Create 3 jsp pages.
login.jsp has the login form. Note that the request method of the form should be same as the requestMethod of the Controller method that will be handling the form request.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> <html> <head> <title>Spring MVC - Login</title> </head> <body> <h2>Login</h2> <form:form method="post" action="processLogin"> <table> <tr> <td><form:label path="userName">User Name</form:label></td> <td><form:input path="userName" /></td> </tr> <tr> <td><form:label path="password">Password</form:label></td> <td><form:password path="password" /></td> </tr> <tr> <td colspan="2"><input type="submit" value="Login" /></td> </tr> </table> </form:form> </body> </html> |
Dispatcher Servlet Configurations
The dispatcher servlet for spring needs to be configured to scan the package for annotation.
Also we need to add the resolvers to resolve the view.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" 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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package="com.kscodes.sampleproject" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"> <value>/WEB-INF/jsp/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean> </beans> |
Output
Enter username = admin and password = admin123 for Success page to be displayed.
Any other values will display the error page.