MVC Architecture
Emily Bauman
Overview
MVC structure is a design pattern for web
applications, with three components:
● Model
● View
● Controller
Overview
Model
● Responsible for maintaining data
● Represents knowledge - can be a single
object or a structure of objects
View
● Responsible for displaying data to user
● Representation of the model
○ presentation filter
○ shows only chosen data to the user
Controller
● Controls interactions between model & view
● Link between user and system
● The “brains” of the application
● Controls data flow into the model and
updates the view when data changes
Simple Example
Model View Controller
HTML CSS Web Browser
● Bare text
● The content, knowledge
of a web page
● Styling
● The display of a web
page
● Brings it all together
● Displays the view with
the knowledge of the
model, allowing
interaction for the user
Why is it used?
● Isolates the business logic from the user
interface
● Allows the application to be skinnable
○ same data with changeable views
Frameworks
Many programming frameworks have adopted
MVC concepts, including:
● Spring
● Django
● Ruby on Rails
● Struts
● ASP.NET
Our project
● Spring Web MVC framework
○ DispatcherServlet
○ ModelAndView object
○ Annotations
DispatcherServlet
● Dispatches requests to controllers
● Integrated with Spring IoC container
Spring MVC
Our Structure
● Model: ModelAndView object
● View: JSP pages
● Controller: Spring MVC controller
Model: ModelAndView
● Used ModelAndView object as our model
● Spring MVC object: represents a model and
view returned by a handler
● Model is a map with <K,V>
View: JSP
● Used JSP pages as the view
● Calls in attributes of the model
Controller: Spring MVC
● Defined with @Controller annotation
● @RequestMapping annotation to map URLs
to particular handler methods
Spring MVC Example
@RequestMapping(value = "/approvetask", method = RequestMethod.POST)
public ModelAndView approveTask() {
ModelAndView model = new ModelAndView();
model.addObject("msg", "You have approved an employee task.");
model.setViewName("mytasks");
return model;
}
Spring MVC Example
<c:if test="${not empty msg}">
<div class="msg">${msg}</div>
</c:if>
Spring MVC Example
<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>
Resources
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html
http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/servlet/ModelAndView.html
https://spring.io/guides/gs/rest-service/

MVC architecture