JDBC與JSP簡單模擬MVC三層架構(gòu)
M是指數(shù)據(jù)模型,V是指用戶界面,C則是控制器。使用MVC的目的是將M和V的實(shí)現(xiàn)代碼分離,從而使同一個程序可以使用不同的表現(xiàn)形式。比如一批統(tǒng)計(jì)數(shù)據(jù)你可以分別用柱狀圖、餅圖來表示。C存在的目的則是確保M和V的同步,一旦M改變,V應(yīng)該同步更新。
模型-視圖-控制器(MVC)是Xerox PARC在八十年代為編程語言Smalltalk-80發(fā)明的一種軟件設(shè)計(jì)模式,至今已被廣泛使用。最近幾年被推薦為Sun公司J2EE平臺的設(shè)計(jì)模式,并且受到越來越多的使用 ColdFusion 和 PHP 的開發(fā)者的歡迎。模型-視圖-控制器模式是一個有用的工具箱,它有很多好處,但也有一些缺點(diǎn)。
MVC是一個設(shè)計(jì)模式,它強(qiáng)制性的使應(yīng)用程序的輸入、處理和輸出分開。使用MVC應(yīng)用程序被分成三個核心部件:模型、視圖、控制器。它們各自處理自己的任務(wù)。
視圖
視圖是用戶看到并與之交互的界面。對老式的Web應(yīng)用程序來說,視圖就是由HTML元素組成的界面,在新式的Web應(yīng)用程序中,HTML依舊在視圖中扮演著重要的角色.
如何處理應(yīng)用程序的界面變得越來越有挑戰(zhàn)性。MVC一個大的好處是它能為你的應(yīng)用程序處理很多不同的視圖。在視圖中其實(shí)沒有真正的處理發(fā)生,不管這些數(shù)據(jù)是聯(lián)機(jī)存儲的還是一個雇員列表,作為視圖來講,它只是作為一種輸出數(shù)據(jù)并允許用戶操縱的方式。
代碼實(shí)現(xiàn):
package com.accp; public class StudentForm { private int studId; private String studName; private String studAge; public int getStudId() { return studId; } public void setStudId(int studId) { this.studId = studId; } public String getStudName() { return studName; } public void setStudName(String studName) { this.studName = studName; } public String getStudAge() { return studAge; } public void setStudAge(String studAge) { this.studAge = studAge; } } |
JSP:
﹤% SimpleDateFormat sf = new SimpleDateFormat("yyyy年MM月dd日"); String dateString = sf.format(new Date()); out.print(dateString); StudentModel smodel = new StudentModel(); ArrayList list = smodel.ListStudent(DbConnection.getConnection()); Iterator it = list.iterator(); StudentForm sform=null; while(it.hasNext()){ sform = (StudentForm)it.next(); %﹥ ﹤tr﹥﹤td﹥﹤%=sform.getStudId() %﹥ ﹤/td﹥﹤td﹥ ﹤%=sform.getStudName() %﹥ |
模型
模型表示企業(yè)數(shù)據(jù)和業(yè)務(wù)規(guī)則。在MVC的三個部件中,模型擁有最多的處理任務(wù)。例如它可能用象EJBs和ColdFusion Components這樣的構(gòu)件對象來處理數(shù)據(jù)庫。被模型返回的數(shù)據(jù)是中立的,就是說模型與數(shù)據(jù)格式無關(guān),這樣一個模型能為多個視圖提供數(shù)據(jù)。由于應(yīng)用于模型的代碼只需寫一次就可以被多個視圖重用,所以減少了代碼的重復(fù)性。
package com.accp; import java.util.ArrayList; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.Statement; import java.sql.ResultSet; public class StudentModel { private final String SQL_LIST = "SELECT studId,studName,studAge FROM StudentInfo"; private final String SQL_ADD = "INSERT INTO StudentInfo(studName,studAge) VALUES(?,?)"; public ArrayList ListStudent(Connection conn) { ArrayList list = null; Statement stmt = null; ResultSet rs = null; try { list = new ArrayList(); stmt = conn.createStatement(); rs = stmt.executeQuery(SQL_LIST); while(rs.next()){ StudentForm sf = new StudentForm(); sf.setStudId(rs.getInt("studId")); sf.setStudName(rs.getString("studName")); sf.setStudAge(rs.getString("studAge")); list.add(sf); } } catch (Exception e) { // TODO: handle exception System.out.println("查詢異常:"+e.toString()); } finally { DbConnection.closeResultSet(rs); DbConnection.closeStatement(stmt); DbConnection.closeConnection(conn); } return list; } } |
數(shù)據(jù)連接:
package com.accp; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import java.sql.PreparedStatement; public class DbConnection { private static String DRIVER_CLASS = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; private static String DATABASE_URL = "jdbc:sqlserver://localhost:1433;databaseName=Student"; private static String DATABASE_USER = "sa"; private static String DATABASE_PASSWORD = "sasa"; public static Connection getConnection (){ Connection conn= null; try { Class.forName(DRIVER_CLASS); conn = DriverManager.getConnection(DATABASE_URL, DATABASE_USER, DATABASE_PASSWORD); System.out.println("SQL2005 連接成功!"); } catch (Exception ex) { System.out.println("2111:" + ex.getMessage()); } return conn; } public static void closeConnection (Connection conn){ try { if (conn != null) { conn.close(); } } catch (Exception e) { System.out.println(e.toString()); } } public static void closeStatement (Statement stmt){ try { if (stmt != null) { stmt.close(); } } catch (Exception e) { System.out.println(e.toString()); } } public static void closeResultSet (ResultSet rs){ try { if (rs != null) { rs.close(); } } catch (Exception e) { System.out.println(e.toString()); } } } |
控制器
控制器接受用戶的輸入并調(diào)用模型和視圖去完成用戶的需求。所以當(dāng)單擊Web頁面中的超鏈接和發(fā)送HTML表單時,控制器本身不輸出任何東西和做任何處理。它只是接收請求并決定調(diào)用哪個模型構(gòu)件去處理請求,然后確定用哪個視圖來顯示模型處理返回的數(shù)據(jù)。
現(xiàn)在我們總結(jié)MVC的處理過程,首先控制器接收用戶的請求,并決定應(yīng)該調(diào)用哪個模型來進(jìn)行處理,然后模型用業(yè)務(wù)邏輯來處理用戶的請求并返回數(shù)據(jù),最后控制器用相應(yīng)的視圖格式化模型返回的數(shù)據(jù),并通過表示層呈現(xiàn)給用戶。
【編輯推薦】