L07 - MVC
L07 - MVC
MVC PATTERN
AND
SHOPPING CART
MVC Pattern
̵ The model-view-controller (MVC) design
pattern specifies that an application consist of
a data model, presentation information, and
control information.
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 37
1
11/25/2019
MVC Pattern
̵ The Model contains only the pure application
data, it contains no logic describing how to present
the data to a user.
̵ The View presents the model’s data to the user.
The view knows how to access the model’s data,
but it does not know what this data means or what
the user can do to manipulate it.
̵ The Controller exists between the view and the
model. It listens to events triggered by the view
and executes the appropriate reaction to these
events.
̵ In most cases, the reaction is to call a method on
the model. Since the view and the model are
connected through a notification mechanism, the
result of this action is then automatically reflected
in the view. 3
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 37
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 37
2
11/25/2019
Controllers
̵ Controllers are what the user interacts with.
̵ They receive a request from the user, decide
what to do, and send a response back.
̵ It’s the only component that interacts with the
models.
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 37
Model
̵ Models are where an application’s data are
stored, usually in a database.
̵ Responsible for storing and retrieving data.
̵ They know nothing about the user interface.
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 37
3
11/25/2019
View
̵ Views are what the user sees on the
screen (i.e. the HTML).
̵ They present the data to the user.
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 37
SHOPPING CART
4
11/25/2019
Shopping cart
̵ Shopping cart in eCommerce assist to visitors
make purchases online. Upon checkout, the
software calculates the total of the order,
including shipping fee and payment
information, etc.
̵ Shoping cart:
▪ Add item.
▪ Update item (color, size, quantity …)
▪ Delete item
▪ Calcutate bill.
▪…
9
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 37
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 37
10
5
11/25/2019
Store data
request Classes
Control that model
servlet business
View process
JSP
View Access and
JSP display data
response View
JSP Database 11
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 37
11
… …
Session data
Session ID
Attribute 1 Item id 1
Attribute 2 Item id 2
… … 12
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 37
12
6
11/25/2019
… …
Session data
Session ID
“Cart” Cart Object
… …
… … 13
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 37
13
Model Class
̵ All information is stored as the variables
̵ Each variable has getter and setter method:
▪ Setter: Takes value as parameter and stores it in
state variable, validate all value put in
▪ Getter: return current value of variable
14
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 37
14
7
11/25/2019
15
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 37
15
16
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 37
16
8
11/25/2019
17
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 37
17
18
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 37
18
9
11/25/2019
Goal:
̵ Separate web programming and business knowledge
as much as possible
19
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 37
19
Shopping Cart
̵ Usually Map of items
▪ Map type in Java
▪ Has methods to put new element to map, get element with
id, and remove element with id
▪ Use product id with key and product object is value
̵ Map element is business model object
▪ Has fields for all relevant data about item purchased
• Set and get methods for each
• One field should be unique identifier (key field)
• Some fields populated by database lookup
▪ May have way to set quantity purchased
20
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 37
20
10
11/25/2019
21
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 37
21
22
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 37
22
11
11/25/2019
23
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 37
23
24
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 37
24
12
11/25/2019
̵ Inside of a table:
▪ Get number of items in cart
▪ Get list of Product ->using for to loop this list
25
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 37
25
26
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 37
26
13
11/25/2019
27
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 37
27
28
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 37
28
14
11/25/2019
29
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 37
29
30
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 37
30
15
11/25/2019
31
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 37
31
32
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 37
32
16
11/25/2019
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 37
33
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 37
34
17
11/25/2019
35
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 37
35
DEMO
36
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 37
36
18
11/25/2019
Q&A
37
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 37
37
19