Đề án MVC Architecture và Struts Framework In Java (J2EE)
Mục Lục
Phần I : Tổng quan ngôn ngữjava.4
1. Lịch sửphát triển của Java.4
2. Mục đích của Java .4
3. Đặc điểm của Java .4
4. Các loại chương trình trong java .5
5. Các thành phần của Java Environment .5
6. Các loại ứng dụng java .5
Phần II : Môi Trường Phát Triển Ứng Dụng Java.6
1. Khái niệm .6
2. Kiến trúc Eclipse.6
3. The Platform runtime.7
4. The workspace .7
5. The Workbench.7
6. Team support .7
7. Help .7
Phần III : Giới thiệu J2EE và các thành phần liên quan.8
1. Applet.8
2. JavaBean .9
3. JSP. 10
4. Servlets.11
Phần IV : Kiến trúc MVC (Model _View_Controller).13
1. Khái niệm.13
2. Mối quan hệgiữa View và controller .14
3. Mối quan hệgiữa Model và view .14
4. Các lợi điểm của MVC .15
5. Hạn chếcủa 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
1. Qui trình thực hiện .16
1.1. View .16
1.2. Controller .17
1.3. Model.20
2. Triển khai ứng dụng .23
2.1. Cơsởdữliệu.23
2.2. Bốtrí các tập tin trên Eclipse.23
2.3. Kết quả.24
Phần VI : Struts Framework .25
1. Struts 1.0 .25
1.1. Khái niệm.25
1.2. Cấu trúc của Struts .26
1.3. Các thành phần chính của một ứng dụng Struts .27
1.4. Các file cấu hình cần thiết đểxây dựng một ứng dụng Struts .27
1.5. Ưu điểm của Struts (so với MVC sửdụng RequestDispatcher) .28
1.6. Nhược điểm của Struts.28
2. Struts 2.0 .29
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
1. Qui trình thực hiện .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
2 . Triển Khai Ứng Dụng .50
2.1.Cơsởdữliệu.50
2.2. Bốtrí các tập tin trên Eclipse.50
ry = 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; Trang 41 MVC Architecture & 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) { Trang 42 MVC Architecture & 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) { Trang 43 MVC Architecture & Struts Framework in J2EE (Java) 10/2007 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 { Trang 44 MVC Architecture & 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")); } Trang 45 MVC Architecture & 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; } } 4. 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(); con = DriverManager.getConnection(connString); Trang 46 MVC Architecture & 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; } } 5. Struts.xml <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" Trang 47 MVC Architecture & Struts Framework in J2EE (Java) 10/2007 ""> <package name="tokenPackage" namespace="/employee" extends="struts-default"> <action name="getEmployeeList" class="net.vietcore.demo.action.ListEmployeeAction" method="execute"> /Employee.jsp /error.jsp --> <action name="EmployeeInput" method="input" class="net.vietcore.demo.action.EmployeeAction"> <result name="input">/Employee.jsp <action name="EmployeeAdd" method="addEmployee" class="net.vietcore.demo.action.EmployeeAction"> <result type="redirect- action">getEmployeeList error.jps <action name="EmployeeDelete" method="deleteEmployee" class="net.vietcore.demo.action.EmployeeAction"> <result type="redirect">getEmployeeList.action <action name="EmployeePrepareUpdate" class="net.vietcore.demo.action.EmployeeAction"> /Update.jsp <interceptor-ref name="paramsPrepareParamsStack"/> <action name="EmployeeUpdate" method="updateEmployee" class="net.vietcore.demo.action.EmployeeAction"> <result type="redirect- action">getEmployeeList Trang 48 MVC Architecture & Struts Framework in J2EE (Java) 10/2007 6. Build.xml: <property name="lib.home" value="${basedir}/web/WEB- INF/lib"/> <property name="tomcat.home" value="C:\Program Files\Apache Software Foundation\Tomcat 5.5"/> <target name="prepare" description="Prepare some necessary steps"> <target name="clean" description="Delete old build and dist directories"> <target name="compile" depends="prepare" description="Compile Java sources"> <javac srcdir="${src.home}" destdir="${build.home}" debug="${compile.debug}" deprecation="${compile.deprecation}" optimize="${compile.optimize}"> --> Trang 49 MVC Architecture & Struts Framework in J2EE (Java) 10/2007 <target name="dist" depends="compile" description="Create binary distribution"> <war destfile="${dist.home}/${ant.project.name}.war" webxml="${web.home}/WEB-INF/web.xml"> --> --> -- > <!--<zipfileset dir="${web.home}/images" prefix="images"/> --> --> II . Triển Khai Ứng Dụng: 1. Cơ sở dữ liệu: 2 . Bố trí các tập tin trên Eclipse : Trang 50 MVC Architecture & Struts Framework in J2EE (Java) 10/2007 Trang 51
File đính kèm:
- Đề án MVC Architecture và Struts Framework In Java (J2EE).pdf