Spring MVC

Sun, Dec 1, 2019

Read in 3 minutes

Let’s see what is spring MVC

What is Spring MVC

It’s a framework to build a web application.

MVC = Model +View +Controller

springMVC

How to create a Spring MVC Project :

Steps to be followed to create a base project for spring MVC:

STEP:1 Create a Maven Project

Select file -> new- maven project in eclipse.

maven1

select maven-archetype-webapp maven2

Add GroupId and artifact id maven3

STEP:2 Add Spring dependencies in POM.XML

<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.theprogrammerguide</groupId>
	<artifactId>springmvcmaven</artifactId>
	<packaging>war</packaging>
	<version>0.1</version>
	<name>springmvcmaven Maven Webapp</name>
	<url>http://maven.apache.org</url>
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
		<!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<version>2.5</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.1.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.1.4.RELEASE</version>
        </dependency>
	</dependencies>
	<build>
		<finalName>springmvcmaven</finalName>
	</build>
</project>

STEP:3 Edit web.xml and add application.servlet.xml (or any name)

Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>Archetype Created Web Application</display-name>
  <!--  Configure Spring MVC Dispatcher Servlet -->
	<servlet>
		<servlet-name>dispatcher</servlet-name>
		<servlet-class>
			org.springframework.web.servlet.DispatcherServlet
		</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<!-- Set up URL mapping for Spring MVC Dispatcher 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>

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"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	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
    	http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
	<!--  component scanning -->
	<context:component-scan base-package="com.theprogrammerguide.springmvcmaven" />
	<!--  formatting and validation support -->
	<mvc:annotation-driven/>
	<!-- Spring MVC view resolver -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/view/" />
		<property name="suffix" value=".jsp" />
	</bean>
</beans>

Here in dispatcher-servlet.xml, we have to configure the component scanning package, validation support, and the view resolver. Here we are setting views path to /WEB-INF/view/ path where all the JSP pages should be inside this view folder.

STEP:4: Add controller and view (JSP) to Test the App:

HomeController.java

package com.theprogrammerguide.springmvcmaven;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HomeController {
	@RequestMapping("/home")
	public String dispayHomePage(){
		return "home";
	}
}

home.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>home!!!!</h2>
</body>
</html>

projectStructure

To run the Project :

eclipse ->right click ->runas ->Maven build mavenrun

First when the server started the web.xml will be loaded and the DispatcherServlet class in the web.xml will start execution. Also in the web.xml, the dispatcher-servlet.xml location will be loaded and that XML content will be executed. All the beans will be created by the container.

When you test the application with this http://localhost:8081/springmvcmaven/ the default index.jsp will be rendered.

When hit the endpoint http://localhost:8081/springmvcmaven/home, you will get the home.jsp view.