What is MVC?

- MVC Pattern stands for Model-View-Controller Pattern.
- It helps in separating the business logic, presentation logic, and navigation logic.
- Models are responsible for encapsulating the application data.
- The Views render a response to the user with the help of the model object.
- Controllers are responsible for receiving the request from the user and calling the back-end services.
Spring vs Spring MVC
Spring Framework is an open source application framework and inversion of control container for the Java platform. Spring MVC (Model–view–controller) is one component within the whole Spring Framework, to support development of web applications.
Why Spring MVC?
- Spring MVC is a Java framework which is used to build web applications.
- It implements all the basic features of a core spring framework like Inversion of Control, Dependency Injection.
- Spring MVC provides an elegant solution to use MVC in spring framework by the help of DispatcherServlet.
Spring MVC request flow

- When user sends a request, the
DispatcherServletfirst receives the request. - The
DispatcherServletconsults theHandlerMappingand invokes theControllerassociated with the request. - The
Controllerprocesses the request by calling the appropriate service methods and returns aModelAndViewobject to theDispatcherServlet. TheModelAndViewobject contains the model data and the view name. - The
DispatcherServletsends the view name to aViewResolverto find the actualViewto invoke. - Now, the
DispatcherServletwill pass the model object to theViewto render the result. - The
View, with the help of the model data, will render the result back to the user.
Spring MVC – Typical Project structure

- The
DispatcherSevlet, as the name indicates, is a single servlet or the front controller that manages the entire request-handling process. When a request is sent to theDispatcherServlet, it delegates the job by invoking the appropriate controllers to process the request. Like any other servlet, theDispatcherServletneeds to be configured in the web deployment descriptor as shown below. The name of the dispatcher servlet configuration comes from 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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
- Here, the servlet name is
mvc-dispatcher. By default, theDispatcherServletwill look for a file name mvc-dispatcher-servlet.xml to load the Spring MVC configuration. This filename is formed by concatenating the servlet name (“mvc-dispatcher“) with “-servlet.xml“. Here, we use the the url-pattern as “.htm” in order to hide the implementations technology to the users.
- As we have listed index.jsp as the welcome file, the index.jsp will be invoked first when we execute the Spring web application. This is the only jsp file outside the WEB-INF directory, and it is here to provide a redirect to the
DispatcherServlet. All the other views should be stored under the WEB-INF directory so that they can be invoked only through the controller process. - The mvc-dispatcher-servlet.xml is shown below. It shows that we have configured annotation driven processing and all the components should be scanned from the base package “com.test”. Here, the
InternalResourceViewResolveris used to resolve the view name to the actual view. The prefix value + view name + suffix value will give the actual view location.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:component-scan base-package="com.test" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:annotation-driven />
</beans>
