The MVC Model
MVC Architecture
• MVC: Model-View-Controller Architecture
• MVC architecture divides applications into three layers –
Model, View, and Controller
MVC (made it simple)
MVC: Advantages
• MVC separates design concerns:
– data persistence and behavior
– presentation
– control
• It decreases the duplication of code
• Makes the application more easy to modify
• You can apply unit-testing to the different components
• Components in MVC are:
– Reusable
– Expressive
MVC: M from Model
• The model represents business data
and logic
• Responsible for example:
• Performing DB queries
• Calculating the total cost
• Processing orders
• Encapsulate of data and behavior
which are independent of presentation
MVC: V from View
• View (Presentation layer)
• Display information according to client
types
• Display result of business logic (Model)
• Not concerned with how the information
was obtained, or from where (that is the
responsibility of Model)
MVC: C from Controller
• Controller (Control layer)
• Serves as the logical connection
between the user's interaction and the
business services on the back
• Responsible for making decisions
among multiple presentations:
• User's language, locale or access
level dictates a different
presentation.
• A request enters the application
through the control layer, it will decide
how the request should be handled
and what information should be
returned
Evolution of MVC (Java context)
• No MVC
• MVC Model 1 (Page-centric)
• MVC Model 2 (Servlet-centric)
• Web application frameworks
– Example: Struts
• Standard-based Web application framework
– JavaServer Faces (JSR-127)
MVC1: Page-centric
MVC2: Servlet-centric
Web-Application Frameworks
• Web-tier applications share common set of functionality:
– Dispatching HTTP requests
– Invoking model methods
– Selecting and assembling views
• Why a Framework?
– Provide classes and interfaces that can be extended by developers
– Frameworks hide the details
– Don't repeat yourself
– Rapid application Development (provide classes and interfaces that can be
extended by developers)
– Easily replace any component
• Examples of Frameworks:
– Apache Struts
– Grails
– Django
– CakePHP
– Ruby on Rails
– Catalyst
– ... ... many others
MVC model using
Servlets&JSP
Steps…
• Create bean(s) to represent the data
• Use a Servlet to handle request
• Populate the bean(s)
• Store the bean in request or session
• Forward request to the JSP
• Extract data from the bean(s)
Request-based data sharing
Session-based data sharing