MVC Notes
MVC Notes
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.
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
<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...>
<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">
<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 .