This tutorial extends Spring MVC beginner tutorial step by step.
Maven project structure
Model class to capture login form details
Step 1: Create a POJO “Login.java” in the package “com.mytutorial.model” under “src/main/java” to capture login form details like “user” & “password”.
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 | package com.mytutorial.model; import java.io.Serializable; public class Login implements Serializable { private static final long serialVersionUID = 1L; private String userId; private String userPwd; public String getUserId() { return userId; } public void setUserId(String userId) { this.userId = userId; } public String getUserPwd() { return userPwd; } public void setUserPwd(String userPwd) { this.userPwd = userPwd; } @Override public String toString() { return "Login [userId=" + userId + ", userPwd=" + userPwd + "]"; } } |
Define the Controller
Step 2: Create a new controller “SimpleLoginController.java” in the package “com.mytutorial.controller”.
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 | package com.mytutorial.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; 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.mytutorial.model.Login; @Controller @RequestMapping("/") public class SimpleLoginController { @RequestMapping(method = RequestMethod.GET) public ModelAndView showForm(ModelMap model) { //pass the viewName, modelName, and model object return new ModelAndView("login", "login-form", new Login()); //login.jsp, } @RequestMapping(value = "/login", method = RequestMethod.POST) public String submit(@ModelAttribute("login-form") Login login, ModelMap model) { System.out.println("User is: " + login.getUserId()); model.addAttribute("loggedInUser", login); return "login-success"; } } |
Define the Views
Step 3: The first view is to show the login form with “login.jsp” under “src/main/webapp/WEB-INF/views”.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Simple page</title> </head> <body> <form:form method="POST" action="login" modelAttribute="login-form"> <form:input type="text" path="userId" id="user" /> <form:input type="password" path="userPwd" id="password" /> <input type="submit" value="go" /> </form:form> </body> </html> |
Step 4: The second view is to display the login success message with “login-success.jsp” under “src/main/webapp/WEB-INF/views”.
The flow
simpleWeb/entry/ –> “SimpleLoginController.showForm()” –> login.jsp –> /simpleWeb/entry/login –> “SimpleLoginController.submit()” –> login-success.jsp
No changes to web.xml, applicationContext.xml & pom.xml
Same as Spring MVC beginner tutorial step by step.
Build the war & deploy to Tomcat
Same as Spring MVC beginner tutorial step by step.
Run it in a browser
http://localhost:8080/simpleWeb/entry/
http://localhost:8080/simpleWeb/entry/login