Spring MVC Tutorial
A Spring MVC is a Java framework which is used to build web applications. It follows the Model-View-Controller design pattern. It implements all the basic features of a core spring framework like Inversion of Control, Dependency Injection.
A Spring MVC provides an elegant solution to use MVC in spring framework by the help of DispatcherServlet. Here, DispatcherServlet is a class that receives the incoming request and maps it to the right resource such as controllers, models, and views.
Spring Web Model-View-Controller
Understanding the flow of Spring Web MVC
Advantages of Spring MVC Framework
Let's see some of the advantages of Spring MVC Framework:-
Spring Web MVC Framework Example
Let's see the simple example of a Spring Web MVC framework. The steps are as follows:
Directory Structure of Spring MVC
Directory Structure of Spring MVC using Maven
Required Jar files or Maven Dependency
To run this example, you need to load:
Download Link: Download all the jar files for spring including JSP and JSTL.
If you are using Maven, you don't need to add jar files. Now, you need to add maven dependency to the pom.xml file.
1. Provide project information and configuration in the pom.xml file.
2. Create the controller class
To create the controller class, we are using two annotations @Controller and @RequestMapping.
The @Controller annotation marks this class as Controller.
The @Requestmapping annotation is used to map the class with the specified URL name.
3. Provide the entry of controller in the web.xml file
In this xml file, we are specifying the servlet class DispatcherServlet that acts as the front controller in Spring Web MVC. All the incoming request for the html file will be forwarded to the DispatcherServlet.
4. Define the bean in the xml file
This is the important configuration file where we need to specify the View components.
The context:component-scan element defines the base-package where DispatcherServlet will search the controller class.
This xml file should be located inside the WEB-INF directory.
5. Display the message in the JSP page
This is the simple JSP page, displaying the message returned by the Controller.
Spring MVC Index
Spring MVC Form
Spring MVC Applications
Spring MVC Validation
Spring MVC Tiles
Next TopicMultiple View Page