Spring MVC

AJAY NEGI
4 min readDec 20, 2020

--

What is Spring MVC?

Spring MVC is a Java framework which is used to build web applications. It follows the model-view-controller design pattern, not just that it also implements all the basic features of core spring framework like inversion of control, dependency injection. Spring MVC provides us a dignified solution to use MVC in the spring framework with the help of a dispatcher servlet. Now you must be thinking “What is dispatcher servlet?”. So, dispatcher servlet is a class that receives the incoming requests and maps it to the right resource such as controller,model and view.

This is a Model View and controller spring framework and it comprised of a web browser, a front controller, a model and a view. So now let me explain you all these components in detail.

Model=Model contains the core data of the application, this data can either be a single object or a group of objects.

Controller=Controller contains the business logic of the application. You can use @Controller annotation to mark this class as a controller .

View=View is used to represent the information in a particular format like in excel format and you can also use JSP that is Java Server pages to create a view page.

Now let’s see how internally spring integrates with the model, view and controller approach.

First, when a request is sent to dispatcher servlet, the request is also sent from dispatcher servlet to handler mapping to controller to view resolver and finally to view. So, when a request is send to a dispatcher servlet, the request is sent to handler mapping and a controller, so when it comes to controller, the controller functions internally and sends the response that will be a model and a view, back to the dispatcher servlet and then this dispatcher servlet sends the view to a view resolver and when it gets resolved in the view resolver functionality then it will be presented as a view. So, after all these functionalities then you will get the output.

So basically the incoming requests are obstructed by the dispatcher servlet that works as a front controller and the dispatcher gets an entry of handler mapping from the XML file forwards the request to the controller and after that controller returns an object of model and view and finally the dispatcher servlet checks the entry of the view resolver in the XML file and then invokes the specified view component. So that’s exactly how it works. Let’s look at a example to understand spring MVC much better.

Example of Spring MVC project

Steps to create the Spring MVC project :

  1. Create a new Maven project and add dependencies.
  2. Create a controller class and configure web.xml file.
  3. Define the bean in XML file.
  4. Create a view page and execute the program.
@Controller
public class Addition{
@RequestMapping("/")
public String display(){
return index;
}
}

The @Controller marks this class as a controller and @RequestMapping is used to map the class with the specified URL So this URL is referring to the view page that is index.jsp which is as follows:

<html>
<body>
<p>Hello World</p>
<body>
</html>

So this message “Hello World” should be executed on the browser using the software that is a tomcat server. After this we have to configure the bean class and the web.xml file.

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<ctx:annotation-config></ctx:annotation-config>
<ctx:component-scan base-package="com.ajay">
<mvc:annotaion-driven>
</beans>

So this is the configuration of all the beans class and defining the bean in an xml file is necessary to specify the view components. In this the context components scan element defines the base package where dispatcher servlet will search the controller class. Now lets look at the web.xml file.

<web-app>
<display-name>My Web Application</display-name>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

Here what I am doing is specifying the servlet class, which is a dispatcher servlet that acts as a front controller in the Spring MVC and all the incoming requests for the html file will be forwarded to the dispatcher servlet.

Conclusion

Spring is a lightweight framework which means there will not be any performance issues. Spring MVC provides high security as you can see most of the online banking web applications are developed using spring MVC because of its high security. I hope that now you would have much clearer understanding of spring MVC than you had before reading this blog. So see ya on the next blog. 😊

--

--

AJAY NEGI
AJAY NEGI

Written by AJAY NEGI

Software Engineer Trainee at Mount blue Technologies.

Responses (1)