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



<html>
	<head>
		<metahttp-equiv="Content-Type"content="text/html; charset=ISO-8859-1">
			<title>Insert title here</title>
		</head>
		<body>
			<formaction="submitForm"method="POST">
				<divalign="center">
					<table>
						<tr>
							<td>User Name</td>
							<td>
								<inputtype="text"name="userName"/>
							</td>
						</tr>
						<tr>
							<td>Password</td>
							<td>
								<inputtype="password"name="password"/>
							</td>
						</tr>
						<tr>
							<td></td>
							<td>
								<inputtype="submit"value="Submit"/>
							</td>
						</tr>
					</table>
				</form>
			</body>
		</html>

 

LoginBean.java


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;
	}
}

web.xml

<web-app>
	<display-name>Archetype Created Web Application</display-name>
	<servlet>
		<servlet-name>appServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/servlet-context.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>appServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
	<welcome-file-list>
		<welcome-file>login.jsp</welcome-file>
	</welcome-file-list>
</web-app>

LoginController.java

LoginController.java
packagecom.spring.controller;

importorg.springframework.stereotype.Controller;
importorg.springframework.ui.Model;
importorg.springframework.web.bind.annotation.ModelAttribute;
importorg.springframework.web.bind.annotation.RequestMapping;
importorg.springframework.web.bind.annotation.RequestMethod;

@Controller
publicclassLoginController {
	
	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public String init(Model model) {
	model.addAttribute("msg", "Please Enter Your Login Details");
	return"login";
	  }
	
	@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());
	return"success";
	      } else {
	model.addAttribute("error", "Invalid Details");
	return"login";
	      }
	    } else {
	model.addAttribute("error", "Please enter Details");
	return"login";
	    }
	  }
	
	

}

servlet-context.xml


<beans:beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver">
	<beans:propertyname="prefix"value="/jsp/"/>
	<beans:propertyname="suffix"value=".jsp"/>
</beans:bean>
<context:component-scanbase-package="com.spring.controller"/>undefined</beans:beans>

Output

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

Comment