How to start with Spring MVC

Knoldus Blog Audio
Reading Time: 3 minutes

In this blog I would like to introduce you to the Spring MVC framework, how it works with very detailed steps and at the end I will show How to create a Spring Controller class and view in a simple project.

What is Spring MVC?

It is a framework of Java that is generally used in order to develop web applications. It is built on a Model view controller pattern. Model-View-Controller(MVC) architecture provides the simplest way to develop flexible and loosely coupled web applications. All the primary features of the mainspring framework such as Inversion of control and dependency Injection are also implemented by Spring MVC.

(If you are unsure of what Spring or Spring Boot is, you might want to read What is Spring Framework?, first.)

Spring Web Model View Controller..

It comprises four main components as shown in the below figure.

Now let’s move further and understand the workflow .

How Spring MVC works?

  • All the incoming request are first received by the Dispatcher Servlet.
  • The Dispatcher Servlet then consults the Handler Mapping and forwards the request to the Controller.
  • The Controller calls the appropriate Service method and returns an object of ModelAndView to the Dispatcher Servlet.
  • The Dispatcher Servlet determines the view from the ModelandView object and passes the model object to the view.
  • The view then returns the results of the user.

Below diagram will make it clearer.

Steps to create a Spring MVC project.

  1. Either load the jar file(Spring core jar file, Spring web jar file, JSP+JSTL jar file) or add maven dependencies.
  2. Create a controller class
  3. The entry of the controller should be provided in the web.xml file.
  4. In the sperate XML file, define Bean.
  5. Output the message in a JSP file.
  6. Start the server and deploy the project.

Required jar files or Maven dependency

To run this example we need to load:

  • Spring Core jar files
  • Spring Web jar files
  • JSP + JSTL jar files (If you are using any other technology then load the corresponding jar files).

Download Link: Download all the jar files for spring including JSP and JSTL.

web.xml

1?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://xmlns.jcp.org/xml/ns/javaee"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
	id="WebApp_ID" version="3.1">

	<display-name>spring-mvc-demo1</display-name>

	<absolute-ordering />

	<!-- Spring MVC Configs -->

	<!-- Step 1: Configure Spring MVC Dispatcher Servlet -->
	<servlet>
		<servlet-name>dispatcher</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/spring-mvc-demo1.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<!-- Step 2: Set up URL mapping for Spring MVC Dispatcher Servlet -->
	<servlet-mapping>
		<servlet-name>dispatcher</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
	
</web-app>

spring-mvc-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">

	<!-- Step 3: Add support for component scanning -->
	<context:component-scan base-package="com.knoldus.springdemo1" />

	<!-- Step 4: Add support for conversion, formatting and validation support -->
	<mvc:annotation-driven/>

	<!-- Step 5: Define 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>

HomeController.java

package com.knoldus.springdemo1.mvc;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HomeController {
	
	@RequestMapping("/")
     public String showPage() {
		return "main-menu";
    	 
    	 
     }
	
}

output:

This is all that I wanted to talk about in this blog post.In the upcoming post I will talk more about Spring , Stay tuned.

References: