ĐỀ ÁN MÔN Xây Dựng Phần Mềm Hướng Đối Tượng

51 286 0
ĐỀ ÁN MÔN Xây Dựng Phần Mềm Hướng Đối Tượng

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

10/2007 Header MVC PageArchitecture of 166.& Struts Framework in J2EE (Java) TRƯỜNG ĐẠI HỌC KHOA HỌC TỰ NHIÊN KHOA CÔNG NGHỆ THÔNG TIN BỘ MÔN CÔNG NGHỆ PHẦN MỀM Y Z ĐỀ ÁN MÔN Xây Dựng Phần Mềm Hướng Đối Tượng MVC Architecture & Struts Framework In Java(J2EE) Tên Đề Án : GVHD : Trần Minh Triết SV thực : • Võ Đức Thiện • Nguyễn Trí Toàn • Nguyễn Thành Trực Tháng 10/2007 Footer Page of 166 Trang (06hc418) (06hc425) (06hc437) Header MVC PageArchitecture of 166.& Struts Framework in J2EE (Java) 10/2007 Mục Lục Phần I : Tổng quan ngôn ngữ java .4 Lịch sử phát triển Java Mục đích Java Đặc điểm Java 4 Các loại chương trình java 5 Các thành phần Java Environment Các loại ứng dụng java Phần II : Môi Trường Phát Triển Ứng Dụng Java Khái niệm Kiến trúc Eclipse The Platform runtime The workspace .7 The Workbench Team support 7 Help .7 Phần III : Giới thiệu J2EE thành phần liên quan .8 Applet JavaBean JSP 10 Servlets 11 Phần IV : Kiến trúc MVC (Model _View_Controller) .13 Khái niệm .13 Mối quan hệ View controller 14 Mối quan hệ Model view 14 Các lợi điểm MVC 15 Hạn chế MVC 15 Phần V : Áp dụng kiến trúc MVC xây dựng ứng dụng triển khai thực tế .16 Qui trình thực 16 1.1 View 16 1.2 Controller 17 1.3 Model 20 Triển khai ứng dụng 23 2.1 Cơ sở liệu 23 2.2 Bố trí tập tin Eclipse 23 2.3 Kết 24 Phần VI : Struts Framework 25 Struts 1.0 25 1.1 Khái niệm 25 1.2 Cấu trúc Struts 26 1.3 Các thành phần ứng dụng Struts .27 1.4 Các file cấu hình cần thiết để xây dựng ứng dụng Struts 27 1.5 Ưu điểm Struts (so với MVC sử dụng RequestDispatcher) .28 1.6 Nhược điểm Struts 28 Struts 2.0 29 Footer Page of 166 Trang Header MVC PageArchitecture of 166.& Struts Framework in J2EE (Java) 10/2007 Phần VI: Áp dụng kiến trúc MVC xây dựng ứng dụng triển khai thực tế 30 Qui trình thực 30 1.1 Các trang hiển thị(View) 30 1.2.Action 37 1.3.Bean 40 1.4.Utils 46 1.5.Struts.xml 47 1.6.Build.xml 49 Triển Khai Ứng Dụng .50 2.1.Cơ sở liệu .50 2.2 Bố trí tập tin Eclipse 50 Website Tham Khảo ¾ www.vietcore.net ¾ www.javavietnam.org ¾ www.sun.java.com ¾ www.apache.com ¾ www.manguon.com Footer Page of 166 Trang Header MVC PageArchitecture of 166.& Struts Framework in J2EE (Java) 10/2007 Phần I Tổng quan ngôn ngữ java Lịch sử phát triển Java • Ra đời Sun Microsystems James Gosling • 1991: Version với tên “Oak” • 1995: Tên thức Java Mục đích Java: • Dùng để phát triển ứng dụng cho thiết bị điện tử “thông minh” • Tạo trang web có nội dung động (web applet) • Hiện nay, Java sử dụng để phát triển nhiều loại ứng dụng khác nhau: sở liệu, mạng, Internet, viễn thông Đặc điểm Java : • • • • • • • • • Giống C/C++ Đơn giản Object-oriented, hướng đối tượng Platform-independent, không phụ thuộc Mạnh Secure, bảo mật Distributed, phân bố Multithreaded, xử lý đa luồng Dynamic, động Chương trình dịch cổ điển Footer Page of 166 Trang Header MVC PageArchitecture of 166.& Struts Framework in J2EE (Java) 10/2007 Chương trình dịch java Các loại chương trình java : • • • • • Applets Command Line Applications GUI Applications Servlets Database Applications Các thành phần Java Environment • • • • • Java language Bytecode definitions Java/Sun class libraries Java Virtual Machine Structure of class files Các loại ứng dụng java : • • • J2SE : Java Standard Edition (Dùng để phát triển ứng dụng vừa nhỏ) J2EE : Java Enterprise Edition (Dùng để phát triển ứng dụng lớn) J2ME : Java Micro Edition (Dụng để phát triển ứng dụng đtdd) Footer Page of 166 Trang Header MVC PageArchitecture of 166.& Struts Framework in J2EE (Java) 10/2007 Phần II Môi Trường Phát Triển Ứng Dụng Java Khái niệm : ™ Eclipse phần mềm miễn phí, nhà phát triển sử dụng để xây dựng ứng dụng J2EE, sử dụng Eclipse nhà phát triển tích hợp với nhiều công cụ hỗ trợ khác để có công cụ hòan chỉnh mà không cần dùng đến phần mềm riêng khác.Eclipse SDK bao gồm phần : Platform, Java Development Toolkit (JDT), Plug-in Development Environment (PDE) Với JDT, Eclipse xem môi trường hỗ trợ phát triển Java mạnh mẽ PDE hỗ trợ việc mở rộng Eclipse, tích hợp Plug-in vào Eclipse Platform Eclipse Platform tảng toàn phần mềm Eclipse, mục đích cung cấp dịch vụ cần thiết cho việc tích hợp công cụ phát triển phần mếm khách dạng Plug-in, thân JDT coi Plug-in làm cho Eclipse Java IDE (Integrated Development Enviroment) Kiến trúc Eclipse : Footer Page of 166 Trang Header MVC PageArchitecture of 166.& Struts Framework in J2EE (Java) 10/2007 The Platform runtime : • Công việc Platform runtime phát xem plug-in có thư mục plug-in Eclipse.Mỗi Plug-in có tập tin Manifest liệt kê kết nối mà plug-in cần.Pug-in tải vào Eclipse thực cần thiết để giảm lượng tài nguyên yêu cầu thời gian khởi tạo The workspace : • • • Workspace chịu trách nhiệm quản lý tài nguyên người dùng tồ chức dạng Project Mỗi Project thư mục thư mục Workspace Workspace bảo quản cấp thấp lịch sử thay đổi tài nguyên, tránh thất thoát tài nguyên người dùng Workspace đồng thời chịu trách nhiệm thông bào công cụ cấn thiết cho việc thay đổi tài nguyên The Workbench : • Workbench giao diện đồ họa người dùng Eclipse, gồm có Standard Widget Toolkit (SWT) JFace Eclipse không hòan toàn bắt buộc phải sử dụng SWT hay Jface để lập trình giao diện, bạn sử dụng AWT hay SWING Java thông qua việc cài đặt Plug-ins Team support : • Trang bị hệ thống quản trị để quản lý dự án người dùng : Concurrent Versions System (CVS) Help : • Cung cấp hệ thống tài liệu mở rộng, định dạng HTML hay XML Cửa sổ eclipse3.2 Footer Page of 166 Trang Header MVC PageArchitecture of 166.& Struts Framework in J2EE (Java) 10/2007 Phần III Giới thiệu J2EE thành phần liên quan (Applet , JavaBean , JSP , Servlet , EJB) Là thành phần quan trọng rộng lớn lĩnh vực java , J2EE dùng để phát triển ứng dụng lớn mang tính chất phân tán rộng khắp Đây lĩnh vực trọng ứng dụng nhiều java thực tế Sơ đồ thành phần J2EE Applet : • • • • • chương trình Java chạy web browser Tất applets phải lớp lớp Applet JApplet Để tạo applet, ta phải import: ¾ java.applet ¾ java.awt Một applet có phương thức Mỗi phương thức tự động gọi có event tương ứng Methods: • init( ) • start( ) • stop( ) • destroy( ) Footer Page of 166 Trang Header MVC PageArchitecture of 166.& Struts Framework in J2EE (Java) 10/2007 Sự khác Applet Application • Applications chạy Java interpreter, applets chạy browser có hổ trợ Java, dùng ‘AppletViewer’ có JDK • Việc thực applications bắt đầu hàm ‘main()’ Còn applet không cần hàm main • Applications dùng ‘System.out.println()’ để xuất, applets dùng hàm ‘drawString()’ để hiển thị • Các hạn chế Security Applets • Không thể đọc viết file hệ thống file user • Không thể trao đổi với site • Không thể chạy chương trình hệ thống hành • Không thể tải chương trình lưu trữ hệ thống user JavaBean: ƒ ƒ ƒ ƒ ƒ ƒ JavaBeans software component model Nó tương tác với đối tượng software khác dùng lại Các software components mà dùng lại gọi ‘beans’ Components dùng distributed environments JavaBeans thiết kế để dùng cần Java thường xây dựng software thông qua JavaBeans Khi mà Java Beans tạo ra, chúng dùng thư viện không phụ thuộc Footer Page of 166 Trang Header MVC PageArchitecture 10 of 166 & Struts Framework in J2EE (Java) 10/2007 Các qui tắc để tạo Beans ƒ Là class ƒ phải có default constructor ƒ Có thuộc tính non-public ƒ Các thuộc tính truy xuất từ bên thông qua hàm getXXX setXXX JSP : (Java Server Pages) ƒ ƒ ƒ JSP technology đươc xây dựng ngôn ngư lap trình Java No thừa ke tat ca cac ưu điem cac đac tính cua ngôn ngư Java JSP co the dung tat ca cac platforms Cách làm việc JSP Các bước xử lý request Footer Page 10 of 166 Trang 10 Header MVC PageArchitecture 37 of 166 & Struts Framework in J2EE (Java) 10/2007 Action : EmployeeAction.class package net.vietcore.demo.action; import net.vietcore.demo.bean.Employee; import net.vietcore.demo.bean.EmployeeService; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.Preparable; import com.opensymphony.xwork2.ModelDriven; public class EmployeeAction extends ActionSupport implements ModelDriven, Preparable{ /** * */ private static final long serialVersionUID = 1L; private int id; private Employee employee; private EmployeeService service = new EmployeeService(); Footer Page 37 of 166 Trang 37 Header MVC PageArchitecture 38 of 166 & Struts Framework in J2EE (Java) 10/2007 public int getId() { return id; } public void setId(int id) { this.id = id; } /* * This method is call automatically when the Class is called * */ public void prepare() throws Exception { System.out.println("prepare() in Employee Action"); System.out.println("EmployeeID = "+id); if (id==0){ employee = new Employee(); System.out.println("Tao moi "); }else { employee = service.findById(id); System.out.println("Tim "); } } /* * This method is call automatically when the Class is called * */ public Object getModel() { System.out.println("getModel() in Employee Action"); return employee; } public String addEmployee(){ System.out.println("addEmployee() in Employee Action"); service.addEmployee(employee); return SUCCESS; } public String deleteEmployee(){ System.out.println("deleteEmployee() in Employee Action"); System.out.println(employee.getId()); service.deleteEmployee(employee.getId()); return SUCCESS; } public String updateEmployee(){ System.out.println("updateEmployee() in Employee Action"); service.updateEmployee(employee); return SUCCESS; Footer Page 38 of 166 Trang 38 Header MVC PageArchitecture 39 of 166 & Struts Framework in J2EE (Java) 10/2007 } public String execute() throws Exception{ System.out.println("execute() in Employee Action"); return SUCCESS; } public String input() throws Exception{ System.out.println("input() in Employee Action"); return INPUT; } } ListEmployeeAction.class package net.vietcore.demo.action; import java.util.List; import net.vietcore.demo.bean.EmployeeService; import com.opensymphony.xwork2.ActionSupport; public class ListEmployeeAction extends ActionSupport{ /** * */ private static final long serialVersionUID = 1L; EmployeeService service = new EmployeeService(); private List employeeList; public List getEmployeeList() { System.out.println("getEmployeeList() in class ListEmployeeAction"); return employeeList; } public String execute() throws Exception{ System.out.println("execute() in class ListEmployeeAction"); service.addEmployeeToList(); employeeList = service.list(); return SUCCESS; } } Footer Page 39 of 166 Trang 39 Header MVC PageArchitecture 40 of 166 & Struts Framework in J2EE (Java) Bean: Employee.class package net.vietcore.demo.bean; public class Employee { private int id; private String name; private String sex; private String birthday; private String city; private String email; private String history; public String getBirthday() { return birthday; } public void setBirthday(String birthday) { this.birthday = birthday; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getHistory() { return history; } public void setHistory(String history) { Footer Page 40 of 166 Trang 40 10/2007 Header MVC PageArchitecture 41 of 166 & Struts Framework in J2EE (Java) 10/2007 this.history = history; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } } EmployeeService.class package net.vietcore.demo.bean; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import net.vietcore.demo.utils.DataUtils; public class EmployeeService { private ArrayList employeeList; public ArrayList list(){ return employeeList; } public void addEmployeeToList(){ //System.out.println("addEmployeeToList() in Employee Action "); ResultSet rs; Footer Page 41 of 166 Trang 41 Header MVC PageArchitecture 42 of 166 & Struts Framework in J2EE (Java) 10/2007 int i=0; //PreparedStatement pstmt = null; try { //DataUtils.connectDb(); //pstmt = DataUtils.getConnect().prepareStatement("Select * from tblEmployee"); rs= DataUtils.Doc("Select * from tblEmployee"); //rs = pstmt.executeQuery(); employeeList = new ArrayList(); while (rs.next()) { i++; Employee employee = new Employee(); employee.setId(rs.getInt("ID")); employee.setName(rs.getString("name")); employee.setSex(rs.getString("sex")); employee.setBirthday(rs.getString("birthday")); employee.setCity(rs.getString("city")); employee.setEmail(rs.getString("email")); employee.setHistory(rs.getString("history")); employeeList.add(employee); } } catch (Exception e) { //e.printStackTrace(); } System.out.println(i); } public int addEmployee(Employee employee){ System.out.println("addEmployee method in EmployeeService class"); int ret = -1; PreparedStatement pstmt = null; try { DataUtils.connectDb(); pstmt = DataUtils.getConnect().prepareStatement("INSERT INTO tblEmployee (Name, Sex, Birthday, City, Email, History) VALUES(?, ?, ?, ?, ?, ?)"); pstmt.setString(1, employee.getName()); pstmt.setString(2, employee.getSex()); pstmt.setString(3, employee.getBirthday()); pstmt.setString(4, employee.getCity()); pstmt.setString(5, employee.getEmail()); pstmt.setString(6, employee.getHistory()); int result = pstmt.executeUpdate(); if(result > 0) { Footer Page 42 of 166 Trang 42 Header MVC PageArchitecture 43 of 166 & Struts Framework in J2EE (Java) 10/2007 System.out.println("Employee insert successfully"); ret = 1; } else { ret = 0; } } catch (Exception e) { e.printStackTrace(); } finally { if(pstmt != null) { try { pstmt.close(); } catch (SQLException e) { e.printStackTrace(); } } DataUtils.disconnectDb(); } return ret; } public int updateEmployee(Employee employee){ System.out.println("updateEmployee method in EmployeeService class"); int ret = -1; PreparedStatement pstmt = null; try { DataUtils.connectDb(); pstmt = DataUtils.getConnect().prepareStatement("UPDATE tblEmployee SET Name=?, Sex=?, Birthday=?, City=?, Email=?, History=? WHERE ID=?"); pstmt.setString(1, employee.getName()); pstmt.setString(2, employee.getSex()); pstmt.setString(3, employee.getBirthday()); pstmt.setString(4, employee.getCity()); pstmt.setString(5, employee.getEmail()); pstmt.setString(6, employee.getHistory()); pstmt.setInt(7, employee.getId()); int result = pstmt.executeUpdate(); if(result > 0) { Footer Page 43 of 166 Trang 43 Header MVC PageArchitecture 44 of 166 & Struts Framework in J2EE (Java) System.out.println("Employee saved successfully"); ret = 1; } else { ret = 0; } } catch (Exception e) { e.printStackTrace(); } finally { if(pstmt != null) { try { pstmt.close(); } catch (SQLException e) { e.printStackTrace(); } } DataUtils.disconnectDb(); } return ret; } public int deleteEmployee(int id){ System.out.println("deleteEmployee method in EmployeeService class"); int ret = -1; PreparedStatement pstmt = null; try { DataUtils.connectDb(); pstmt = DataUtils.getConnect().prepareStatement("DELETE FROM tblEmployee WHERE id=?"); pstmt.setInt(1, id); pstmt.executeUpdate(); ret = 1; } catch (Exception e) { e.printStackTrace(); } finally { Footer Page 44 of 166 Trang 44 10/2007 Header MVC PageArchitecture 45 of 166 & Struts Framework in J2EE (Java) 10/2007 if(pstmt != null) { try { pstmt.close(); } catch (SQLException e) { e.printStackTrace(); } } DataUtils.disconnectDb(); } return ret; } public Employee findById(int id){ addEmployeeToList(); System.out.println("findById method in EmployeeService class"); for (Employee employee : employeeList) { if(employee.getId()==id) return employee; } return null; } public Employee getEmployee(int id){ Employee employee = new Employee(); ResultSet rs = null; PreparedStatement pstmt = null; try { DataUtils.connectDb(); pstmt = DataUtils.getConnect().prepareStatement("SELECT * FROM tblEmployee WHERE id=?"); pstmt.setInt(1, id); rs = pstmt.executeQuery(); while (rs.next()) { employee.setId(rs.getInt("ID")); employee.setName(rs.getString("Name")); employee.setSex(rs.getString("Sex")); employee.setBirthday(rs.getString("Birthday")); employee.setCity(rs.getString("City")); employee.setEmail(rs.getString("Email")); employee.setHistory(rs.getString("History")); } Footer Page 45 of 166 Trang 45 Header MVC PageArchitecture 46 of 166 & Struts Framework in J2EE (Java) 10/2007 } catch (Exception e) { e.printStackTrace(); } finally { if(pstmt != null) { try { pstmt.close(); } catch (SQLException e) { e.printStackTrace(); } } if(rs != null) { //rs.close(); } DataUtils.disconnectDb(); } return employee; } } Utils : DataUtils.class package net.vietcore.demo.utils; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class DataUtils { private static Connection con; private static int count; public static void connectDb() { try { // Connect Normal String DBDriver = "sun.jdbc.odbc.JdbcOdbcDriver"; String connString = "jdbc:odbc:dataStruts2"; Class.forName(DBDriver).newInstance(); = DriverManager.getConnection(connString); Footer Page 46 of 166 Trang 46 Header MVC PageArchitecture 47 of 166 & Struts Framework in J2EE (Java) 10/2007 // chuong trinh DEMO System.out.println("Connection number: " + (++count)); } catch (ClassNotFoundException cnfe) { try { throw new Exception("Cannot find the specified driver.", cnfe); } catch (Exception e) { } } catch (Exception e) { System.out.println("Error, class Connect \n Method: connectDb()"); System.out.println(e); } } public static Connection getConnect() { return con; } public static void disconnectDb() { try { System.out.println("Disconnecting " + count); con.close(); } catch (SQLException sqle) { System.out.println(sqle.getMessage()); } } public static ResultSet Doc(String chuoi_lenh) throws Exception { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection conn= DriverManager.getConnection("jdbc:odbc:dataStruts2"); Statement stmt = conn.createStatement(); ResultSet rs= stmt.executeQuery(chuoi_lenh); return rs; } } Struts.xml /Employee.jsp /error.jsp > /Employee.jsp getEmployeeList error.jps getEmployeeList.action /Update.jsp getEmployeeList Footer Page 48 of 166 Trang 48 Header MVC PageArchitecture 49 of 166 & Struts Framework in J2EE (Java) 10/2007 Build.xml: > Footer Page 49 of 166 Trang 49 Header MVC PageArchitecture 50 of 166 & Struts Framework in J2EE (Java) 10/2007 > > -> > > II Triển Khai Ứng Dụng: Cơ sở liệu: Bố trí tập tin Eclipse : Footer Page 50 of 166 Trang 50 Header MVC PageArchitecture 51 of 166 & Struts Framework in J2EE (Java) Footer Page 51 of 166 Trang 51 10/2007 ... Các thành phần hợp thành giao diện người sử dụng Bởi MVC đòi hỏi giao diện người sử dụng ứng dụng cấu trúc thành đối tượng kế thừa định nghĩa quan hệ chuẩn đối tượng này, phiên chung đối tượng có... login.jsp gửi hành động login đối tượng action lớp LoginAction triệu gọi Đối tượng action cài đặt giao diện chuẩn (Action) với phương thức execute, có đối số đối tượng Request Response Phương... thành phần giao diện kết hợp với view controller thành đối tượng đơn WACT nổ lực để đưa tập thành phần phong phú tương tự việc phát triển web mà trì tách biệt view controller Các thành phần thúc

Ngày đăng: 24/03/2017, 06:27

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan