Spring MVC Login Form Using Maven Example

In this following example, simply I have created a login form using Spring MVC framework. Spring provides different types of annotations to handling forms. Here are the typical annotations.

@Controller :

  • @Controller is an annotation, used in Spring MVC Framework.
  • @Controller annotation is a sub annotation of @Cmponent annotation.
  • We can use @Controller annotation on top of the class only.
  • It indicates that a particular class serves the role of controller in MVC pattern.
  • @Controller annotation acts as a stereotype for the annotated class, indicating its roles.

@RequestMapping :

  • Like @Controller annotation, @RequestMapping annotation is also used in Spring MVC Framework.
  • @RequestMapping annotation can be applied on top of the class and/or methods, and it should be used along with the @Controller annotation.
  • It is used to bind a http request to spring components or handler methods.
  • Class level mapping binds a specific request to the Spring component (class), whereas method level mapping binds a specific request to a specific handler method.

@ModelAttribute :

  • @ModelAttribute annotation can be used on top of a method or method arguments.
  • An @ModelAttribute on a method is used to populate the model with commonly needed attributes for example to fill a drop-down with states.
  • @ModelAttribute on method arguments refers a specific property in Model class (MVC).
  • Used to bind the data from form to controller
  • It should be used along with @RequestMapping in Controllers
  • @ModelAttribute is invoked before the actual controller method starts execution along with @RequestMapping

Here is the project structure:

No Image

Let’s begin from login.jsp

		<metahttp-equiv="Content-Type"content="text/html; charset=ISO-8859-1">
			<title>Insert title here</title>
							<td>User Name</td>



package com.spring.controller;

public class LoginBean {
	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;


	<display-name>Archetype Created Web Application</display-name>




publicclassLoginController {
	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public String init(Model model) {
	model.addAttribute("msg", "Please Enter Your Login Details");
	@RequestMapping(method = RequestMethod.POST)
	public String submit(Model model, @ModelAttribute("loginBean") LoginBeanloginBean) {
	if (loginBean != null&&loginBean.getUserName() != null&loginBean.getPassword() != null) {
	if (loginBean.getUserName().equals("sneha") &&loginBean.getPassword().equals("admin")) {
	model.addAttribute("msg", loginBean.getUserName());
	      } else {
	model.addAttribute("error", "Invalid Details");
	    } else {
	model.addAttribute("error", "Please enter Details");





Our Services:

Java Training | Python Training | Machine Learning with Python Training | Deep Learning with Python Training | Data Science with Python Training | Projects

SILAN Technology, BBSR
Call to: 0674-2361252