Spring MVC Example with Annotation | Login Application in Spring MVC

By Arvind Rai, June 28, 2019
Spring provides Web MVC (model-view-controller) framework. That is known as Spring MVC. Spring MVC works on the basis of DispatcherServlet. Data binding in spring is highly flexible and any object can be used as a command. There will be a controller in spring MVC which will be annotated by @Controller. The entire request will be bound by @RequestMapping in a controller. Spring MVC has clear separation of different roles as controller, validator, model object, form object etc. In our example we will show a complete login application in Spring MVC.

Let�s start from our controller. Every controller must be annotated by @Controller. Controller has two methods. First one will serve the login page request and second one will implement login logic ad redirect the login success and failure message. Every method which is serving request must be annotated by @RequestMapping.

LoginController.java
package com.concretepage.controller;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class LoginController {
	@RequestMapping(value="login", method = RequestMethod.GET)
	public String login(){
		return "redirect:pages/login.jsp";
	}
	@RequestMapping(value="pages/userCheck", method = RequestMethod.POST)
	public String userCheck(ModelMap model, HttpServletRequest request) {
		String name=request.getParameter("name");
		String pwd=request.getParameter("pwd");
		if("concretepage".equalsIgnoreCase(name)&&"concretepage".equalsIgnoreCase(pwd)){
			model.addAttribute("message", "Successfully logged in.");
			
		}else{
			model.addAttribute("message", "Username or password is wrong.");
		}
		return "redirect:success.jsp";
	}
}
 
Now create spring xml named as dispatcher-servlet.xml. All the controllers will be kept in a package and will be configured by component-scan in dispatcher-servlet.xml. We need to define prefix and suffix of the URL that will be managed by org.springframework.web.servlet.view.InternalResourceViewResolver. Find the dispatcher-servlet.xml.

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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans  
   http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context 
   http://www.springframework.org/schema/context/spring-context.xsd">
   <context:component-scan base-package="com.concretepage.controller" />
   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	  <property name="prefix" value="/pages/"/>
	  <property name="suffix" value=".jsp"/> 
   </bean>
</beans>
 
To enable Spring MVC, we need to configure web.xml for org.springframework.web.servlet.DispatcherServlet and org.springframework.web.context.ContextLoaderListener.

web.xml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<web-app 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_4.xsd"
    version="2.4">
	<display-name>Spring MVC Application</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>/</url-pattern>
	</servlet-mapping>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
</web-app>
 
Configure pom.xml for the Spring MVC dependencies. Find the pom.xml.

pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.concretepage.app</groupId>
  <artifactId>spring-mvc</artifactId>
  <version>1</version>
  <packaging>war</packaging>      
  <name>SpringMVC</name>
  <dependencies>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-web</artifactId>
		<version>3.1.0.RELEASE</version>
		<type>jar</type>
		<scope>compile</scope>
	</dependency>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-webmvc</artifactId>
		<version>3.1.0.RELEASE</version>
		<type>jar</type>
		<scope>compile</scope>
	</dependency>
	<dependency>
		<groupId>jstl</groupId>
		<artifactId>jstl</artifactId>
		<version>1.2</version>
		<type>jar</type>
		<scope>compile</scope>
	</dependency>
	<dependency>
		<groupId>taglibs</groupId>
		<artifactId>standard</artifactId>
		<version>1.1.2</version>
		<type>jar</type>
		<scope>compile</scope>
	</dependency>
	<dependency>
		<groupId>javax.servlet</groupId>
		<artifactId>servlet-api</artifactId>
		<version>3.0-alpha-1</version>
		<type>jar</type>
		<scope>compile</scope>
	</dependency>
 </dependencies>
 </project> 
Now find the jsp for login and login success message.

login.jsp
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head><title>Login</title></head>
<body>
  <form:form action="userCheck" method="post">
   Enter User Name:<input type="text" name="name"> <br/>
   Enter Password :<input type="password" name="pwd"/><br/>
   <input type="submit">
  </form:form>
</body>
</html> 


success.jsp
Login Message : ${param.message}
 
Deploy and run the application. In the login form, enter username and password �concretepage� to get success message.

Download Source Code
spring-mvc-example-with-annotation.zip
POSTED BY
ARVIND RAI
ARVIND RAI







©2024 concretepage.com | Privacy Policy | Contact Us