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

  • Published on
    16-Apr-2015

  • View
    151

  • Download
    4

Embed Size (px)

DESCRIPTION

Ti liu v MVC 2 quater 7 NIIT

Transcript

<p>Ni dung trnh by Gii thiu m hnh MVC 2</p> <p> BookOnline MVC 2</p> <p>2</p> <p>Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012</p> <p>GII THIU M HNH MVC 2</p> <p>3</p> <p>Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012</p> <p>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</p> <p>4</p> <p>Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012</p> <p>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</p> <p>5</p> <p>Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012</p> <p>MVC 1 Model: DAO + POJO</p> <p> View: JSP Controller: JSP</p> <p>6</p> <p>Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012</p> <p>MVC 2 Model: DAO + POJO</p> <p> View: JSP Controller: Servlet</p> <p>7</p> <p>Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012</p> <p>MVC 2 Model: DAO + POJO</p> <p> Cp nht d liu + Rt trch d liu Controller : SERVLET Gi Model cp nht d liu + rt trch d liu</p> <p> Chn View v gi d liu cho View View: JSP Gi cc s kin cho Controller</p> <p> Nhn d liu t Controller v hin th</p> <p>8</p> <p>Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012</p> <p>MVC 2Gi s kin</p> <p>CONTROLLER SERVLETChn View</p> <p>VIEWGi d liu</p> <p>JSP</p> <p>Cp nht d liu Rt trch d liu Hin th d liu</p> <p>MODELDAO9</p> <p>POJO</p> <p>Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012</p> <p>Controller (Servlet) gi d liu cho View (JSP) Request</p> <p> Session Application (ServletContext)</p> <p>10</p> <p>Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012</p> <p>Controller (Servlet) REQUEST View (JSP)</p> <p>Page1Forward</p> <p>URL ngi dng nhn thy: Page1 Cha cc thuc tnh trong request ca Page1 Attribute1, Attribute2</p> <p>Page2Forward</p> <p>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</p> <p>Page311</p> <p>Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012</p> <p>Controller (Servlet) REQUEST View (JSP)CONTROLLER SERVLETObject value1 = ... Object value2 = ... request.setAttribute (Key1, value) request.setAttribute (Key2, value) ...</p> <p>Forward</p> <p>VIEW JSP12</p> <p>Object value1 = request.getAttribute (Key1) Object value2 = request.setAttribute (Key2) ...</p> <p>Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012</p> <p>Controller (Servlet) REQUEST View (JSP) Controller (Servlet)</p> <p>POJO pojo = . . .request.setAttribute("key", pojo); String url="url view";</p> <p>RequestDispatcher rd =request.getRequestDispatcher(url); rd.forward(request, response);</p> <p>13</p> <p>Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012</p> <p>Controller (Servlet) REQUEST View (JSP) View (JSP)</p> <p> Java Bean</p> <p>. . . Scriptlet 14</p> <p>Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012</p> <p>Controller (Servlet) REQUEST View (JSP) Controller (Servlet)</p> <p>ArrayList ds = . . .request.setAttribute("key", ds); String url="url view";</p> <p>RequestDispatcher rd =request.getRequestDispatcher(url); rd.forward(request, response);</p> <p>15</p> <p>Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012</p> <p>Controller (Servlet) REQUEST View (JSP) View (JSP)</p> <p> Java Bean</p> <p>. . . Scriptlet 16</p> <p>Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012</p> <p>Controller (Servlet) SESSION View (JSP) SessionSession ID 1 Session 1 Client 1</p> <p>Server Session ID 2 Session 2 Client 2</p> <p>17</p> <p>Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012</p> <p>Controller (Servlet) SESSION View (JSP) Forward</p> <p> Ngi dng khng nhn thy URL thay i Ngi dng khng th Bookmark Qu trnh chuyn d liu Servlet JSP</p> <p> SendRedirect Ngi dng nhn thy URL thay i Ngi dng c th Bookmark trang JSP</p> <p> Qu trnh chuyn d liu JSP Servlet JSP Bn View cn kim tra d liu tn ti cha18</p> <p>Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012</p> <p>Controller (Servlet) SESSION View (JSP)CONTROLLER SERVLETObject value1 = ... Object value2 = ... HttpSession session = request.getSesssion(); session.setAttribute (Key1, value) session.setAttribute (Key2, value) ...</p> <p>Forward</p> <p>VIEW JSP19</p> <p>Object value1 = session.getAttribute (Key1) Object value2 = session.setAttribute (Key2) ...</p> <p>Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012</p> <p>Controller (Servlet) SESSION View (JSP) Controller (Servlet)</p> <p>POJO pojo = . . .HttpSession session = request.getSession(); session.setAttribute("key", pojo);</p> <p>String url="url view";RequestDispatcher rd = request.getRequestDispatcher(url);</p> <p>rd.forward(request, response);20</p> <p>Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012</p> <p>Controller (Servlet) SESSION View (JSP) View (JSP)</p> <p> Java Bean</p> <p>. . . Scriptlet 21</p> <p>Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012</p> <p>Controller (Servlet) SESSION View (JSP) Controller (Servlet)</p> <p>ArrayList ds = . . .HttpSession session = request.getSession(); session.setAttribute("key", ds);</p> <p>String url="url view";RequestDispatcher rd = request.getRequestDispatcher(url);</p> <p>rd.forward(request, response);22</p> <p>Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012</p> <p>Controller (Servlet) SESSION View (JSP) View (JSP)</p> <p> Java Bean</p> <p>. . . Scriptlet 23</p> <p>Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012</p> <p>Controller (Servlet) APPLICATION View (JSP) SessionSession ID 1 Application (ServletContext) Client 1</p> <p>Server Session ID 2 Client 2</p> <p>24</p> <p>Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012</p> <p>Controller (Servlet) APPLICATION View (JSP) Forward</p> <p> Ngi dng khng nhn thy URL thay i Ngi dng khng th Bookmark Qu trnh chuyn d liu Servlet JSP</p> <p> SendRedirect Ngi dng nhn thy URL thay i Ngi dng c th Bookmark trang JSP</p> <p> Qu trnh chuyn d liu JSP Servlet JSP Bn View cn kim tra d liu tn ti cha25</p> <p>Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012</p> <p>Controller (Servlet) APPLICATION View (JSP)CONTROLLER SERVLETsynchronized(this) { Object value1 = ... Object value2 = ... ServletContext application = this.getServletContext(); application.setAttribute (Key1, value) application.setAttribute (Key2, value) ... }</p> <p>Forward</p> <p>VIEW JSP26</p> <p>Object v1 = application.getAttribute (Key1) Object v2 = application.setAttribute (Key2) ...</p> <p>Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012</p> <p>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 =</p> <p>request.getRequestDispatcher(url);rd.forward(request, response);27</p> <p>Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012</p> <p>Controller (Servlet) APPLICATION View (JSP) View (JSP)</p> <p> Java Bean</p> <p>. . . Scriptlet 28</p> <p>Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012</p> <p>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 =</p> <p>request.getRequestDispatcher(url);rd.forward(request, response);29</p> <p>Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012</p> <p>Controller (Servlet) APPLICATION View (JSP) View (JSP)</p> <p> Java Bean</p> <p> Scriptlet</p> <p>30</p> <p>Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012</p> <p>BOOKONLINE MVC 2</p> <p>31</p> <p>Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012</p> <p>BookOnline Trang ng nhp</p> <p>32</p> <p>Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012</p> <p>Trang ng nhp MVC 2 Controller - Servlet</p> <p> DangNhap.do Controller - JSP DangNhap.jsp</p> <p> URL ngi dng chn t website DangNhap.do?action=Nap</p> <p>33</p> <p>Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012</p> <p>Trang ng nhp MVC 2Gi s kin</p> <p>CONTROLLER DangNhap.doChn View</p> <p>VIEW DangNhap.jspGi d liu</p> <p>URL: DangNhap.do?action=NapHin th d liu</p> <p>Rt trch d liu</p> <p>MODEL DAO POJO</p> <p>34</p> <p>Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012</p> <p>Trang ng nhp MVC 2 Ngi dng chn trang ng nhp bng cch chn URL ng nhp </p> <p>35</p> <p>Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012</p> <p>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);</p> <p>36</p> <p>Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012</p> <p>Trang ng nhp MVC 2 DangNhap.jsp</p> <p>action 1 2 3 4 5 6 7 8 937</p> <p> . . . Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012</p> <p>Trang ng nhp MVC 2 DangNhap.jsp</p> <p>CONTROLLER</p> <p>DangNhap.do</p> <p>38</p> <p>Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012</p> <p>Trang ng nhp MVC 2 DangNhap.do</p> <p>VIEW DangNhap.jsp CONTROLLER DangNhap.do</p> <p>CONTROLLER TrangXXX.do</p> <p>39</p> <p>Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012</p> <p>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</p> <p>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){ String loi =Tn ng nhp hoc mt khu khng ng; request.setAttribute(loi, loi); url = DangNhap.jsp; }else{ HttpSession session = request.getSession(); session.setAttribute(KhachHang, kh); String url=TrangXXX.do; response.sendRedirect(url); return; } } RequestDispatcher rd = request.getRequestDispatcher(url); rd.forward(request, response);Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012</p> <p>Trang ng nhp MVC 2 DangNhap.jsp</p> <p>1 2 3 4 5 6 7 8 941</p> <p> ... } %&gt;Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012</p> <p>Ti liu tham kho Nguyn Hong Anh, Tp bi ging mn chuyn Java, 2011 Marty Hall, Tp bi ging J2EE, 2010</p> <p>42</p> <p>Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012</p> <p>HI V P</p> <p>43</p> <p>Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012</p>