MVC Pattern and Framework - Tutorial Presentation
MVC Pattern and Framework - Tutorial Presentation
MVC Pattern
(MVC Framework)
1
08/02/2006
Agenda
? Layered (or tiered) application design
? Introduction of MVC pattern
? Evolution of Web Application design
architecture
– Model 1
– Model 2
– Application frameworks
This is the agenda. First, we will talk about general concept behind
layered (or tiered) application architecture. Then we will discuss how
MVC pattern fits in with this layered architecture.
During the rest of the session, we will discuss the evolution of web
application design architecture starting with Model 1, then talk about
Model2 and finally application frameworks.
2
08/02/2006
Layered Application
Design
3
08/02/2006
- Respond to clients
4
4
08/02/2006
Processing
Layer
5
08/02/2006
Now let's talk about why you want to use layered application design?
The layered design also provides a common place where pre and post
processing such as logging, translations and transformations can be
performed.
6
08/02/2006
Introduction to
MVC Pattern
7
08/02/2006
MVC Pattern
This picture shows MVC pattern, in which Model, View, and Controller
components are shown and how they interact.
8
08/02/2006
? Processing orders
9
08/02/2006
10
10
08/02/2006
11
11
08/02/2006
Web Applications
? It is often advantageous to treat each layer
as an independent portion of your
application
? Do not confuse logical separation of
responsibilities with actual separation of
components
? Some or of the layers can be combined into
single components to reduce application
complexity
12
12
08/02/2006
Evolution of Web
Application Design
Architecture
13
Now let's talk about the evolution of web application design architecture.
13
08/02/2006
14
14
08/02/2006
So in the first phase of the evolution, just static HTML pages were used to
display static information. Then in the subsequent evolution phase,
dynamic contents generation technologies such as CGI initially, then
servlet and JSP are introduced to generate and display dynamic contents
along with static contents.
When you are using JSP pages, you can use only the basic features that
come with it. Or you can leverage more sophisticated features such as
component-based dynamic contents generation, for example, leveraging
JavaBeans or custom tags, which provide more reusable, more
maintainable, more flexible development and deployment options.
Model 1
(Page-Centric
Architecture)
16
16
08/02/2006
1
Request
JSP
4 pages
BROWSER
Response
3
Java Bean
Servlet Enterprise
Container Information Systems
(EIS) 17
The literature on Web-tier technology in the J2EE platform frequently uses the terms
“Model 1” and “Model 2” without explanation. This terminology stems from early
drafts of the JSP specification, which described two basic usage patterns for JSP
pages. While the terms have disappeared from the specification document, they
remain in common use.
Page-centric Architecture
? Composed of a series of interrelated JSP pages
– JSP pages handle all aspects of the application -
presentation, control, and business process
? Business process logic and control decisions are
hard coded inside JSP pages
– in the form of JavaBeans, scriptlets, expression
? Next page selection is determined by
– A user clicking on a hyper link, e.g. <A HERF="find.jsp>
– Through the action of submitting a form, e.g. <FORM
ACTION="search.jsp">
18
18
08/02/2006
Page-centric Architecture
dataBase
19
08/02/2006
20
08/02/2006
Commodore 1541
Capacity 180k
details.jsp Cost: 200
Aligened: sometimes
[main][logoff]
footer.jsp
21
Component design
21
08/02/2006
Now if you decide to use page centric approach, here are a few
suggestions.
22
08/02/2006
Page-centric Scenario
View
search.html
Request 1
response list.j s p
Client
Model
response
forward
JavaBeans
Request 2
Controller
find .jsp
Request 3
redirect
delete.jsp
23
23
08/02/2006
Model 2
(Servlet-Centric
Architecture)
24
24
08/02/2006
1
Request
(Controller)
Servlet
BROWSER
Ins
Redirect 3
tan
2
tia
te
5
(View) (Model)
Java Bean
Response JSP 4
25
08/02/2006
26
26
08/02/2006
Servlet-centric Architecture
? JSP pages are used only for presentation
– Control and application logic handled by a servlet (or
set of servlets)
? Servlet serves as a gatekeeper
– Provides common services, such as authentication,
authorization, login, error handling, and etc
? Servlet serves as a central controller
– Act as a state machine or an event dispatcher to decide upon
the appropriate logic to handle the request
– Performs redirecting
27
In servlet-centric architecture, JSP pages are used only for presentation and
control and business process are handled by a servlet or a set of servlets.
27
08/02/2006
28
One question people ask is who many servlets are desired to build servlet-
centric architecture. The answer is of course “it depends”. It depends on
the granularity of your application. For a simple application, you could
have a single master servlet that handles everything or you could have
multiple servlets each of which handles different business function. Or
you could build a combination of these two approach in which you have a
master servlet that handles common functions such as common login for all
business functions and then delegate to child servlets for further
processing.
28
08/02/2006
Servlet-centric Scenario
View
search.html
Request 1
response list.jsp
Client
Model
response
forward
JavaBeans
Request 2
Controller
Request 3
servlet
29
29
08/02/2006
Now let's see some techniques you can use for dispatching and saving data in
model 1 and model 2 architectures.
30
08/02/2006
31
08/02/2006
32
This slide shows how servlet stores data in a JavaBean and then saves it as
a Request scope variable from which JSP page will later retrieves data.
32
08/02/2006
33
This slide shows how servlet stores data in a JavaBean and then saves it as
a session scope variable from which JSP page will later retrieves data.
33
08/02/2006
34
This slide shows how servlet stores data in a JavaBean and then saves it as
an application context scope variable from which JSP page will later
retrieves data.
34
08/02/2006
Even though most relatively sophisticated Web applications are expected to use
Model 2 architecture, let's compare them here anyway.
35
08/02/2006
Model 1 (Page-centric)
? May encourage spaghetti JSP pages
– Business logic may get lost in the display pages
? Use JavaBeans or custom tags that captures
36
One issue with Model 1 architecture is that because the page selection is
done by each JSP page and business logic processing is done within JSP
pages, it may encourage spaghetti JSP pages. The recommended approach
if you are going to use page-centric approach is to capture business logic in
the form of JavaBeans or custom tags.
In general, JSP pages are easier to write than servlets but it is harder to
debug since the actual code is generated by the JSP compiler.
36
08/02/2006
Model 2 (Servlet-centric)
? Loosens the coupling between the pages and
improves the abstraction between presentation
and application logic
– Use JSPs for pure data display and input collection
activities
– Most of the business logic can be debugged
through the servlet before passed to JavaBeans and
JSP
37
37
08/02/2006
38
38
08/02/2006
How Do I Decide?
? Use page-centric
– If the application is simple enough that links from page to
page.
?
Use servlet-centric
– Each link or button click requires a great deal of processing and
decision-making about what should be displayed next.
? “How mapping between requests and responses
are done” can help you to decide
– Each request maps to one and only one response
? No need for controller.
– Each request spawns a great deal of logic and a variety of
different views can result
? A servlet is ideal 39
39
08/02/2006
Web Application
Frameworks
40
40
08/02/2006
41
Now as people are gaining more experience, they found that most Model 2
architecture based web applications share a common set of functionality.
For example, they all do receive and dispatch HTTP requests, invoking
model methods, selecting and assembling views.
Well, if everybody is doing the same thing over and over every time they
write Model 2 based application, then people thought why don't we create
a common framework that support these set of functionality so only thing
you have to do is basically using or extending the frameworks using
common interface and classes.
41
08/02/2006
42
In a more concrete terms, these are the benefits of using a common Web
application frameworks. Framework decouples presentation tier from
business logic, which would be useful for maintenance and reusability
of the code. It provides a central point of control. Popular frameworks
also come with other extra features
42
08/02/2006
43
43
08/02/2006
44
44
08/02/2006
45
45
08/02/2006
JSF S1AF
JSTL
Struts
JSP ™
java bean, expression
lang.,custom tag lib
Servlet
session tracking, filtering, listener
08/02/2006
Passion!
47
47