0% found this document useful (0 votes)
410 views

MVC Notes

The document provides an overview of the Model-View-Controller (MVC) architectural pattern. It describes the three main components - the model, the view, and the controller. The model manages the application's data and business logic. The view is responsible for rendering the user interface. The controller handles input/output between the model and view. It provides examples of how each component would work in a customer management application. The document then discusses how the MVC pattern is implemented in the Spring MVC framework.

Uploaded by

Keerthana Reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
410 views

MVC Notes

The document provides an overview of the Model-View-Controller (MVC) architectural pattern. It describes the three main components - the model, the view, and the controller. The model manages the application's data and business logic. The view is responsible for rendering the user interface. The controller handles input/output between the model and view. It provides examples of how each component would work in a customer management application. The document then discusses how the MVC pattern is implemented in the Spring MVC framework.

Uploaded by

Keerthana Reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Model-View-Controller (MVC) 

The Model-View-Controller (MVC) is an architectural pattern that separates an application


into three main logical components: the model, the view, and the controller. Each of these
components are built to handle specific development aspects of an application. MVC is one of
the most frequently used industry-standard web development framework to create scalable and
extensible projects.

MVC Components
Model: The Model component corresponds to all the data related logic that the user works with.
This can represent either the data that is being transferred between the View and Controller
components or any other business logic related data. For example, a Customer object will
retrieve the customer information from the database, manipulate it and update it data back to the
database or use it to render data.

View: The View component is used for all the UI logic of the application. For example, the
Customer view would include all the UI components such as text boxes, dropdowns, etc. that
the final user interacts with.

Controller: Controllers act as an interface between Model and View components to process all
the business logic and incoming requests, manipulate data using the Model component and
interact with the Views to render the final output. For example, the Customer controller would
handle all the interactions and inputs from the Customer View and update the database using the
Customer Model. The same controller would be used to view the Customer data.

MVC Framework - Architecture


We studied the high-level architecture flow of MVC Framework. Now let us have a look at how
the execution of an MVC application takes place when certain request comes from the client.
The diagram below shows the flow:
MVC Flow Diagram

Flow Steps
 The client browser sends request to the MVC Application.
 Global.ascx receives this request and performs routing based on the URL of incoming
request using the RouteTable, RouteData, UrlRoutingModule and MvcRouteHandler
objects.
 This routing operation calls the appropriate controller and executes it using the
IControllerFactory object and MvcHandler object's Execute method.
 The Controller processes the data using Model and invokes the appropriate method using
ControllerActionInvoker object
 The processed Model is then passed to the View which in turn renders the final output.

The Spring web MVC framework provides model-view-controller architecture and ready
components that can be used to develop flexible and loosely coupled web applications. The
MVC pattern results in separating the different aspects of the application (input logic, business
logic, and UI logic), while providing a loose coupling between these elements.
 The Model encapsulates the application data and in general they will consist of POJO.
 The View is responsible for rendering the model data and in general it generates HTML
output that the client's browser can interpret.
 The Controller is responsible for processing user requests and building appropriate
model and passes it to the view for rendering.

The DispatcherServlet

The Spring Web model-view-controller (MVC) framework is designed around


aDispatcherServlet that handles all the HTTP requests and responses. The request processing
workflow of the Spring Web MVC DispatcherServlet is illustrated in the following diagram:
Following is the sequence of events corresponding to an incoming HTTP request
to DispatcherServlet:
 After receiving an HTTP request, DispatcherServlet consults theHandlerMapping to call
the appropriate Controller.
 The Controller takes the request and calls the appropriate service methods based on used
GET or POST method. The service method will set model data based on defined
business logic and returns view name to the DispatcherServlet.
 The DispatcherServlet will take help from ViewResolver to pickup the defined view for
the request.
 Once view is finalized, The DispatcherServlet passes the model data to the view which is
finally rendered on the browser.
All the above mentioned components ie. HandlerMapping, Controller and ViewResolver are
parts of WebApplicationContext which is an extension of the plain ApplicationContext with
some extra features necessary for web applications.
Required Configuration
You need to map requests that you want the DispatcherServlet to handle, by using a URL
mapping in the web.xml file. The following is an example to show declaration and mapping
for HelloWeb DispatcherServlet example:
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<display-name>Spring MVC Application</display-name>

<servlet>
<servlet-name>HelloWeb</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>HelloWeb</servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>

</web-app>
The web.xml file will be kept WebContent/WEB-INF directory of your web application. OK,
upon initialization of HelloWeb DispatcherServlet, the framework will try to load the
application context from a file named [servlet-name]-servlet.xml located in the
application's WebContent/WEB-INFdirectory. In this case our file will be HelloWeb-
servlet.xml.
Next, <servlet-mapping> tag indicates what URLs will be handled by the which
DispatcherServlet. Here all the HTTP requests ending with .jsp will be handled by
the HelloWeb DispatcherServlet.
If you do not want to go with default filename as [servlet-name]-servlet.xmland default location
as WebContent/WEB-INF, you can customize this file name and location by adding the servlet
listener ContextLoaderListener in your web.xml file as follows:
<web-app...>

<!-------- DispatcherServlet definition goes here----->


....
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/HelloWeb-servlet.xml</param-value>
</context-param>

<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>
Now, let us check the required configuration for HelloWeb-servlet.xml file, placed in your web
application's WebContent/WEB-INF directory:
<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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:component-scan base-package="com.tutorialspoint" />

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>

</beans>
Following are the important points about HelloWeb-servlet.xml file:
 The [servlet-name]-servlet.xml file will be used to create the beans defined, overriding
the definitions of any beans defined with the same name in the global scope.
 The <context:component-scan...> tag will be use to activate Spring MVC annotation
scanning capability which allows to make use of annotations like @Controller and
@RequestMapping etc.
 The InternalResourceViewResolver will have rules defined to resolve the view names.
As per the above defined rule, a logical view named hellois delegated to a view
implementation located at /WEB-INF/jsp/hello.jsp .

You might also like