intTypePromotion=1
zunia.vn Tuyển sinh 2024 dành cho Gen-Z zunia.vn zunia.vn
ADSENSE

Bài 2. Tổng quan MVC

Chia sẻ: Cao Tuấn | Ngày: | Loại File: PDF | Số trang:47

101
lượt xem
28
download
 
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

Xây dựng nhiều servlet Trong servlet: xử lý nghiệp vụ (business logic), hiển thị.Sử dụng JSP cho việc hiển thị Servlet forward các yêu cầu tới JSP Tách business logic và presentation.Đưa business logic ra khỏi servlet và đặt trong Model –các lớp Java có thể sử dụng lại Model: sự kết hợp của business data và các phương thức theo tác trên dữ liệu

Chủ đề:
Lưu

Nội dung Text: Bài 2. Tổng quan MVC

  1. Bài 2. Tổng quan MVC
  2. Nội dung  1. Ví dụ về MVC  2. Giới thiệu về thiết kế MVC  3. Quá trình phát triển của kiến trúc ứng dụng Web  3.1. Model 1  3.2. Model 2  3.3. Application frameworks 2
  3. Bài toán 3
  4. Giải pháp ban đầu  Xây dựng nhiều servlet  Trong servlet: xử lý nghiệp vụ (business logic), hiển thị 4
  5. Giải pháp +: sử dụng JSP  Sử dụng JSP cho việc hiển thị  Servlet forward các yêu cầu tới JSP  Tách business logic và presentation 5
  6. Giải pháp ++: sử dụng MVC  Đưa business logic ra khỏi servlet và đặt trong Model – các lớp Java có thể sử dụng lại  Model: sự kết hợp của business data và các phương thức theo tác trên dữ liệu 6
  7. Giải pháp ++: sử dụng MVC 7
  8. Ví dụ 1. Tư vấn bia Trang html Trang JSP 8
  9. 9
  10. beer_v1 Cấu trúc thư mục lập trình src web com form.html result.html WEB-INF example web.xml web model BeerSelect BeerExpert .java .java 10
  11. beer_v1 webapps tomcat WEB-INF form.html result.html classes com web.xml example web model Cấu trúc thư mục triển khai BeerSelect BeerExpert .class .class 11
  12. form.html
  13. web.xml ServletBeer com.example.web.BeerSelect ServletBeer /SelectBeer.do 13
  14. Servlet BeerSelect – version 1 public class BeerSelect extends HttpServlet { @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("Beer Selection Advice"); String c = request.getParameter("color"); out.println("Got beer color "+c); } } 14
  15. Test ứng dụng 15
  16. Model BeerExpert public class BeerExpert { public List getBrands(String color){ List brands = new ArrayList(); if(color.equals("amber")){ brands.add("Jack Amber"); brands.add("Red Moose"); } else{ brands.add("Jail Pale Ale"); brands.add("Gout Stout"); } return brands; } } 16
  17. Servlet BeerSelect – version 2 import package com.example.web; … protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String c = request.getParameter("color"); BeerExpert be = new BeerExpert(); List result = be.getBrands(c); response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("Beer Selection Advice"); Iterator it = result.iterator(); while(it.hasNext()){ out.print("try: "+it.next()); } } 17
  18. Test ứng dụng 18
  19. Kiến trúc hiện tại 19
  20. Kiến trúc mong muốn 20
ADSENSE

CÓ THỂ BẠN MUỐN DOWNLOAD

 

Đồng bộ tài khoản
2=>2