Mô hình MVC 2 (Model-View-Controller)

Embed Size (px)

DESCRIPTION

Tài liệu về MVC 2 quater 7 NIIT

Text of Mô hình MVC 2 (Model-View-Controller)

Ni dung trnh by Gii thiu m hnh MVC 2

BookOnline MVC 2

2

Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012

GII THIU M HNH MVC 2

3

Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012

Java Web Servlet mnh v x l nghip v v iu phi nhng li rt yu v hin th JSP mnh v x l hin th nhng li yu v x l nghip v v iu phi

4

Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012

Java Web Thng th chng ta s kt hp sc mnh ca Servlet v JSP vo m hnh MVC 2 (Model-View-Controller) Cc Servlet ng vai tr lm Controller Cc trang JSP ng vai tr lm View

5

Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012

MVC 1 Model: DAO + POJO

View: JSP Controller: JSP

6

Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012

MVC 2 Model: DAO + POJO

View: JSP Controller: Servlet

7

Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012

MVC 2 Model: DAO + POJO

Cp nht d liu + Rt trch d liu Controller : SERVLET Gi Model cp nht d liu + rt trch d liu

Chn View v gi d liu cho View View: JSP Gi cc s kin cho Controller

Nhn d liu t Controller v hin th

8

Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012

MVC 2Gi s kin

CONTROLLER SERVLETChn View

VIEWGi d liu

JSP

Cp nht d liu Rt trch d liu Hin th d liu

MODELDAO9

POJO

Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012

Controller (Servlet) gi d liu cho View (JSP) Request

Session Application (ServletContext)

10

Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012

Controller (Servlet) REQUEST View (JSP)

Page1Forward

URL ngi dng nhn thy: Page1 Cha cc thuc tnh trong request ca Page1 Attribute1, Attribute2

Page2Forward

URL ngi dng nhn thy: Page1 Cha thuc tnh trong request ca Page1 + Page2 Attribute1, Attribute2 Attribute3URL ngi dng nhn thy: Page1 Cha thuc tnh trong request ca + Page1 + Page2 + Page 3 Attribute1, Attribute2, Attribute3 Attrubute4, Attribute5

Page311

Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012

Controller (Servlet) REQUEST View (JSP)CONTROLLER SERVLETObject value1 = ... Object value2 = ... request.setAttribute (Key1, value) request.setAttribute (Key2, value) ...

Forward

VIEW JSP12

Object value1 = request.getAttribute (Key1) Object value2 = request.setAttribute (Key2) ...

Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012

Controller (Servlet) REQUEST View (JSP) Controller (Servlet)

POJO pojo = . . .request.setAttribute("key", pojo); String url="url view";

RequestDispatcher rd =request.getRequestDispatcher(url); rd.forward(request, response);

13

Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012

Controller (Servlet) REQUEST View (JSP) View (JSP)

Java Bean

. . . Scriptlet 14

Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012

Controller (Servlet) REQUEST View (JSP) Controller (Servlet)

ArrayList ds = . . .request.setAttribute("key", ds); String url="url view";

RequestDispatcher rd =request.getRequestDispatcher(url); rd.forward(request, response);

15

Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012

Controller (Servlet) REQUEST View (JSP) View (JSP)

Java Bean

. . . Scriptlet 16

Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012

Controller (Servlet) SESSION View (JSP) SessionSession ID 1 Session 1 Client 1

Server Session ID 2 Session 2 Client 2

17

Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012

Controller (Servlet) SESSION View (JSP) Forward

Ngi dng khng nhn thy URL thay i Ngi dng khng th Bookmark Qu trnh chuyn d liu Servlet JSP

SendRedirect Ngi dng nhn thy URL thay i Ngi dng c th Bookmark trang JSP

Qu trnh chuyn d liu JSP Servlet JSP Bn View cn kim tra d liu tn ti cha18

Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012

Controller (Servlet) SESSION View (JSP)CONTROLLER SERVLETObject value1 = ... Object value2 = ... HttpSession session = request.getSesssion(); session.setAttribute (Key1, value) session.setAttribute (Key2, value) ...

Forward

VIEW JSP19

Object value1 = session.getAttribute (Key1) Object value2 = session.setAttribute (Key2) ...

Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012

Controller (Servlet) SESSION View (JSP) Controller (Servlet)

POJO pojo = . . .HttpSession session = request.getSession(); session.setAttribute("key", pojo);

String url="url view";RequestDispatcher rd = request.getRequestDispatcher(url);

rd.forward(request, response);20

Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012

Controller (Servlet) SESSION View (JSP) View (JSP)

Java Bean

. . . Scriptlet 21

Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012

Controller (Servlet) SESSION View (JSP) Controller (Servlet)

ArrayList ds = . . .HttpSession session = request.getSession(); session.setAttribute("key", ds);

String url="url view";RequestDispatcher rd = request.getRequestDispatcher(url);

rd.forward(request, response);22

Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012

Controller (Servlet) SESSION View (JSP) View (JSP)

Java Bean

. . . Scriptlet 23

Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012

Controller (Servlet) APPLICATION View (JSP) SessionSession ID 1 Application (ServletContext) Client 1

Server Session ID 2 Client 2

24

Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012

Controller (Servlet) APPLICATION View (JSP) Forward

Ngi dng khng nhn thy URL thay i Ngi dng khng th Bookmark Qu trnh chuyn d liu Servlet JSP

SendRedirect Ngi dng nhn thy URL thay i Ngi dng c th Bookmark trang JSP

Qu trnh chuyn d liu JSP Servlet JSP Bn View cn kim tra d liu tn ti cha25

Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012

Controller (Servlet) APPLICATION View (JSP)CONTROLLER SERVLETsynchronized(this) { Object value1 = ... Object value2 = ... ServletContext application = this.getServletContext(); application.setAttribute (Key1, value) application.setAttribute (Key2, value) ... }

Forward

VIEW JSP26

Object v1 = application.getAttribute (Key1) Object v2 = application.setAttribute (Key2) ...

Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012

Controller (Servlet) APPLICATION View (JSP) Controller (Servlet)synchronized(this) { POJO pojo = . . . ServletContext application = this.getServletContext(); application.setAttribute("key", pojo); String url="url view"; RequestDispatcher rd =

request.getRequestDispatcher(url);rd.forward(request, response);27

Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012

Controller (Servlet) APPLICATION View (JSP) View (JSP)

Java Bean

. . . Scriptlet 28

Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012

Controller (Servlet) APPLICATION View (JSP) Controller (Servlet)synchronized(this) { ArrayList ds = . . . ServletContext application = this.getServletContext(); application.setAttribute("key", ds); String url="url view"; RequestDispatcher rd =

request.getRequestDispatcher(url);rd.forward(request, response);29

Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012

Controller (Servlet) APPLICATION View (JSP) View (JSP)

Java Bean

Scriptlet

30

Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012

BOOKONLINE MVC 2

31

Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012

BookOnline Trang ng nhp

32

Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012

Trang ng nhp MVC 2 Controller - Servlet

DangNhap.do Controller - JSP DangNhap.jsp

URL ngi dng chn t website DangNhap.do?action=Nap

33

Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012

Trang ng nhp MVC 2Gi s kin

CONTROLLER DangNhap.doChn View

VIEW DangNhap.jspGi d liu

URL: DangNhap.do?action=NapHin th d liu

Rt trch d liu

MODEL DAO POJO

34

Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012

Trang ng nhp MVC 2 Ngi dng chn trang ng nhp bng cch chn URL ng nhp

35

Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012

Trang ng nhp MVC 2 DangNhap.do1 2 3 4 5 6 7 8 9 String action = request.getParameter(action); String url=""; if (action.equals(Nap)){ url = DangNhap.jsp; } RequestDispatcher rd = request.getRequestDispatcher(url); rd.forward(request, response);

36

Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012

Trang ng nhp MVC 2 DangNhap.jsp

action 1 2 3 4 5 6 7 8 937

. . . Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012

Trang ng nhp MVC 2 DangNhap.jsp

CONTROLLER

DangNhap.do

38

Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012

Trang ng nhp MVC 2 DangNhap.do

VIEW DangNhap.jsp CONTROLLER DangNhap.do

CONTROLLER TrangXXX.do

39

Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012

Trang ng nhp MVC 2 DangNhap.do1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 2240

String action = request.getParameter(action); String url=""; if (action.equals(Nap)){ url = DangNhap.jsp;} else if (action.equals(DangNhap)){ //S dng hidden String tenDangNhap = request.getParameter(tenDangNhap); String matKhau = request.getParameter(matKhau); KhachHang kh = KhachHangDAO.dangNhap(tenDangNhap, matKhau); if(kh==null){ St

Recommended

View more >