Spring mvc hello world example with Spring 5 and maven

In this article, We are going to discuss spring MVC hello world example with Spring 5 and maven.

Introduction about Spring MVC :

The Spring Web model view controller (MVC) framework is designed around a DispatcherServlet that dispatches requests to handlers, with configurable handler mappings, view resolution, locale, and theme resolution as well as support for uploading files.

The default handler is based on the @Controller and @RequestMapping annotations, offering a wide range of flexible handling methods.

DispatcherServlet :

Spring DispatcherServlet is the class which manages the entire request handling process.

Similiar to normal servlet we can configure the DispatcherServlet in web deployment Descriptor(web.xml). By default, DispatcherServlet will look for a name dispatcher-servlet.xml to load the Spring MVC configuration.

We can completely integrate with all the component of spring. This example will explain DispatcherServlet and its configuration.

Spring MVC hello world example with Spring 5:

MVC stands for model, views, and controller. below we can see the meaning of the components.

  • Models – Domain objects that are processed by the service layer or the persistent layer.
  • Views – Display data end user, normally it’s a JSP page written with the Java Standard Tag Library (JSTL).
  • Controllers – URL mapping and interact with service layer for business processing and return a Model.

Technologies used:

  • Java 1.8
  • Tomcat 8.0.37
  • Spring MVC 5.x.x
  • Your favorite IDE

Step1 :

Create a maven project as mention below image details.

Spring MVC hello world example with Spring 5.
Spring MVC hello world example with Spring 5.

Step 2: Directory Structure

A standard Maven project directory structure.

Project Structure
Project Structure

Step 3: Adding Spring MVC dependencies

 

Step 4: Configuring the Spring MVC application

In this example, we are configuring two configuration files those are web.xml and dispatcher-servlet.xml file. In web.xml we are declaring the DispatcherServlet as we can see in below XML. 

File: web.xml

 

File:dispatcher-servlet.xml

 

Step 4: Controller Class

Create a controller class as HelloController.java. In the code below, @RequestMapping annotation maps web requests onto specific handler classes and/or handler methods.

Step 5: JSP file

Create the helloworld.jsp file under /WEB-INF/views/ directory.

Step 6: Output

Spring MVC hello world example with Spring 5.
Spring MVC hello world example with Spring 5.

 

Happy Learning !!!!!

Related Articles :

Leave a Reply

Your email address will not be published. Required fields are marked *

Show Buttons
Hide Buttons