版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、一種基于Hibernate、Struts 和Spring的J2EE應(yīng)用開發(fā)框架隨著 Java 技術(shù)的逐漸成熟與完善 , 作為建立企業(yè)級應(yīng)用的標準平臺 ,J2EE 平 臺 得到了長足的發(fā)展。借助于 J2EE規(guī)范中包含的多項技術(shù):Enterprise JavaBean(EJB) 、 Java Servlets(Servlet) 、 Java Server Pages(JSP) 、 Java MessageService(JMS)等,開發(fā)出了許多應(yīng)用系統(tǒng)。但是,在傳統(tǒng) J2EE應(yīng)用的開 發(fā)過 程中也出現(xiàn)了一些問題:(1) 數(shù)據(jù)模型和邏輯模型之間的矛盾。目前使用的數(shù)據(jù)庫基本上都是關(guān)系型數(shù)據(jù)庫,而Jav
2、a本質(zhì)上是一種面向?qū)ο蟮恼Z言,對象在存儲和讀取時使用SQ和JDB(進行數(shù)據(jù)庫操作,降低了編程的效率以及系統(tǒng)的可維護性;(2)傳統(tǒng)的J2EE應(yīng)用多采用基于EJB的重量級框架,這種框架適合于開發(fā)大型企業(yè)應(yīng)用,但是使用EJB容器進行開發(fā)和調(diào)試需要耗費大量時間。為了降低代碼的耦合性,提高系統(tǒng)的開發(fā)效率,本文提出了一種基于 Struts 框架、 Hibernate 框架和 Spring框架的J2EE應(yīng)用開發(fā)策略。Hibernate 框架Hibernate 是一個數(shù)據(jù)持久層框架,是一種實現(xiàn)對象和關(guān)系之間映射( O/RMapping)的工具,它對JDB(進行了輕量級的對象圭寸裝,使程序員可以使用對象編程思想
3、來操作數(shù)據(jù)庫。它不僅提供了從Java 類到數(shù)據(jù)表的映射,也提供了數(shù)據(jù)查詢和恢復機制。相對于使用JDBCSS座操作數(shù)據(jù)庫,使用Hibernate能大大的 提高實現(xiàn)的 效率。 Hibernate 框架用配置文件的形式來定義Java 對象和數(shù)據(jù)表之 間的映射關(guān)系,同時在更深的層面將數(shù)據(jù)表之間的關(guān)系解釋為 Java 對象之間的繼 承及包含等關(guān)系。通過使用 HQ語句將復雜的關(guān)系算法用對象的方式描述,在很 大程 度上簡化了對數(shù)據(jù)的查詢,加快了開發(fā)的效率。在 Hibernate 中有一個簡單而直觀的API,用于對數(shù)據(jù)庫所表示的對象執(zhí)行查詢。要創(chuàng)建或修改這些對象,只需在程序中與它們進行交互,然后告訴Hiber
4、nate 保存即可。這樣,大量圭裝 持久化操作的業(yè)務(wù)邏輯不再需要編寫煩瑣的JDB(語句,從而使數(shù)據(jù)持久層得到了極大的簡化。用 Struts 實現(xiàn) MV架構(gòu) MVC( Model-View-Controller) 由 Trygve Reenskaug提出,首先被應(yīng)用在SmallTalk-80 環(huán)境中,是許多交互和界面系統(tǒng)的構(gòu)成基礎(chǔ)。根據(jù)界面設(shè)計可變性的需求,MV把交互系統(tǒng)的組成分解成模型、視圖、控制器 三部分。模型(Model)是軟件所處理問題邏輯在獨立于外在顯示內(nèi)容和形式情況下的內(nèi)在抽象, 封裝了問題的核心數(shù)據(jù)、 邏輯和功能的計算關(guān)系, 獨立于具體 的界面表達和I/O操作。視圖(View)把表
5、示模型數(shù)據(jù)及邏輯關(guān)系和狀態(tài)的信息及特定形式展示給用戶。 它從模型獲得顯示信息, 對于相同的信息可以有多個不 同的顯示形式或視圖。控制器( Controller )是處理用戶與軟件的交互操作的, 其職責是控制提供模型中任何變化的傳播,確保用戶界面于模型間的對應(yīng)聯(lián)系; 它接受用戶的輸入,將輸入反饋給模型, 進而實現(xiàn)對模型的計算控制, 是使模型 和視圖協(xié)調(diào)工作的部件。 通常一個視圖對應(yīng)一個控制器。 模型、視圖與控制器的 分離,使得一個模型可以具有多個顯示視圖。 如果用戶通過某個視圖的控制器改 變了模型的數(shù)據(jù),所有其它依賴于這些數(shù)據(jù)的視圖都應(yīng)反映到這些變化。因此, 無論何時發(fā)生了何種數(shù)據(jù)變化, 控制器
6、都會將變化通知所有的視圖, 導致顯示的 更新。這實際上是一種模型的變化 - 傳播機制。Struts 框架Struts 框架最早是作為 ApacheJakarta 項目的組成部分問世運做, 它繼承了MV(的各項特t并根據(jù)J2EE勺特點,做了相應(yīng)的變化與擴展。Struts框架很好 的結(jié)合了 Jsp , Java Servlet , Java Bean , Taglib 等技術(shù)。在Struts 中,承擔MVC中控制器角色的是ActionServlet 。 ActionServlet是一個通用的控制組件。這個 控制組件提供了處理所有發(fā)送到Struts的HTTP#:的入口點,它截取和分發(fā)這些請求到相應(yīng)的
7、動作類 (這些動作類都是 Action 類的子類) 。另外控制組件也負責用相應(yīng)的請求參數(shù)填充 Action Form(FromBear),并傳給動作類(ActionBean )。動作類訪問核心商業(yè)邏輯,即訪問 Java Bear或調(diào)用EJB最后動作類把控制權(quán)傳 給后續(xù)的JSP文件,由JSP文件生成視圖。所有這些控制邏輯利用Struts-corfig.xml 文件來配置。在 Struts框架中,視圖主要由JSF生成頁面完成,Struts提供豐富的JSP標簽庫,這有利于分開表現(xiàn)邏輯和程序邏輯。模型以一個 或多個 Java Bear 的形式存在。在Struts 中,主要存在三種 Bear, 分別是:
8、 Actior ,ActiorForm , EJB或者Java Bear o Struts 框架沒有具體定義模型層的實現(xiàn),在 實際開發(fā)中,模型層通常是和業(yè)務(wù)邏輯緊密相連的, 并且要對底層數(shù)據(jù)進行操作。Hibernate 引入到 Struts 框架的模型層中,使用它來進行數(shù)據(jù)封裝和映射,提供持久化的支持。Spring 框架Spring 明顯區(qū)別于其他輕量級application framework, 它將專注于 web 的支持,與 struts 和 webwork 有著明顯的區(qū)別。在與 struts 、 webwork 的對比上 ,Spring 是一個服務(wù)于所有層面的 即plication fr
9、amework: 提供了 bean的配置基礎(chǔ),AOPtJ支持,JDBC勺提取框 架,抽象事務(wù)支持等等。 它有一個非常顯著的特點: 在某個層面上如果你不需要 Spring 的支持,你就可以不使用 Spring 的 class ,只使用它的某一部分的功能。從它的設(shè)計理念,你可以看到 Spring 幫助你實現(xiàn)了真正的邏輯層和 web 層的分 離。例如:一個校驗應(yīng)用將不用依靠 controllers 就可以實現(xiàn)。 這樣的目標更好 的重用 和易測 , 過分依靠不必要的容器和框架將不能實現(xiàn)這一點。當然,Spring有自己的web支持和通??蚣苣J郊氈峦暾H欢?Spring 替換struts、webwo
10、rk或者其他的web方案非常容易。對于 Spring的web支持或者 不同的地方,Spring允許你在web容器里面建立一個中間層,在測試環(huán)境或者標準獨立的應(yīng)用里面來設(shè)置重用你的商務(wù)邏輯?還有在J2EE環(huán)境里面,你的商務(wù)邏輯不必依靠容器提供的服務(wù),像JTA ,EJB的支持。良好的構(gòu)架的web應(yīng) 用可以運行在任何容器上,如, Tomcat 或者 Resin 。值得注意的是, Spring 不 是和 已經(jīng)存在的解決方案進行競爭 . 我們鼓勵結(jié)合標準的技術(shù), 如, Servlet,JSP, JTA,JNDI,JDBCa nd JDO和非常匹配的工具,如, Hibernate, Velocity, L
11、og4J, and CauchosHessian/Burlap 。這個框架的的設(shè)計思想是在你的應(yīng)用需要改良 的時候, 你將會做一些技術(shù)的選擇,例如 , 如果你需要分布式事務(wù)處理,你可能 需要用 Spring 的 Jta TransactionManager 來實現(xiàn) JTA 服務(wù)?;蛘哂?DataSource TransactionManager or HibernateTransactionManager 來實現(xiàn)的單個數(shù)據(jù)庫交 換。Spring 框架通過配置操作mappings, 展示 resolution, 本地化和模版集成 圍繞著分派請求操作的servlet-DispatcherServl
12、et設(shè)計的。缺省的操作是一個非常簡單的控制接口 , 他只提供了 ModelAndView handleRequest (request, response) 方法。這將用于一個應(yīng)用的控制 , 但是 , 如果你想包含多個層次的控制, AbstractController,AbstractCommandController,MultiActionController,SimpleFormController,AbstractWizardFormControll er將幫助你完成. 應(yīng)用控制將代表那些子系統(tǒng)。注意,你可以選擇一個適當?shù)幕?: 如果你沒有 webform, 你就不必用 FormCon
13、troller ,這就是和 Struts 最大的 不同。 你可以通過命令或者form 對象來操作任何對象,這不需要接口工具或者一個基礎(chǔ)類的驅(qū)動。 Spring 的數(shù)據(jù)綁定非常靈活。舉例來說, 它描述了具有在應(yīng) 用范圍內(nèi)的校驗錯誤的輸入機制 , 但不是系統(tǒng)錯誤,所以在你的 form 對象里面你 不必復制你 的業(yè)務(wù)對象的 spring 屬性 , 只操作出錯的子任務(wù), 或者適當?shù)剞D(zhuǎn)換spring 。換句話說 , 它通??梢院芎玫闹苯咏壎愕臉I(yè)務(wù)對象。 這也是和 struts 圍繞請求基礎(chǔ) 類 Action 和 ActionForm ( 每個 action 操作類型 ) 建立主要的不 同之一。對 比W
14、ebWork,Sping更多的區(qū)別在于對象角色:Spring支持控制器的感 念,一個操作命 令或者 form 對象和得到數(shù)據(jù)傳遞給視圖的模式, 這個模式通常包 含命令和 form 對象 , 但有時也包含任意的參考數(shù)據(jù)。換句話說, 一個 WebWork Action 聯(lián)合所有這些角色到一個單獨的對象。WebWorl允許你用已經(jīng)存在的業(yè)務(wù) 對象作為你form的一部分)但是只生成各自Action的bean屬性。最后,操作請求的Action 實例在一個視 圖里面獲得付值和 form population 。然而 , 參考數(shù)據(jù) 也需要作為 Action 的屬性 被模擬。 關(guān)于視圖 : Spring 的視
15、圖方案非常的靈活。一個控制器執(zhí)行可以通過 response返回ModelAndView對象null就可以直接寫到一個視圖里,在一般情況下)一個ModelAndView實例結(jié)合了一個 view和一個 model Map,包含了 bean name和通訊 對象 (像命令或者form, 參考數(shù)據(jù)等等) 。 View 名稱是非常高端的配置,不是通過bean name, 個properties 文件就是通過你自己的 ViewResolver. 這個抽象的model Map 允許你在視圖層面完成提取, 沒有任何的爭辯JSP/Velocity 或者其他 ,每一種都可以直接完整使用 . 這個 model Ma
16、p 還可以簡 單得得到適當?shù)母袷交瘮?shù)據(jù) 的轉(zhuǎn)換,像JSP請求屬性或者Velocity 模版模式.如果你不想用Spring的web MVC且是想借用Spring嫁接其他的解決方案 , 你可 以非常簡單地通過Spring 繼承你自己的 web 框架, 你可以非常簡單地通過ContextLoaderListener 啟動一個Spring root application context,并且, 通過 Struts 或者 WebWork的 action 利用 ServletContext 屬性(或者 Spring的helper 方法)存取它。值得注意的是, 這里沒有任何的 plugins 被調(diào)用 ,
17、因此沒有專門的集成:來自web層的視圖,你可以簡單的將Spring作為一個管理 即plication context 實例入口點的類庫。所有你注冊bean 和 Spring 服務(wù)都可以在不需要Spring的webMVCl情況下都可以輕松掌握。Spring不是和Struts 、WebWo這些應(yīng)用進行競爭,它將作為一個純web框架應(yīng)用于很多領(lǐng)域,因為它不需要配置數(shù)據(jù)存儲和事務(wù)操作,所以,你可以利用 Spring 提供的中間層和數(shù)據(jù)存儲層來豐富你的應(yīng)用,甚至用JDBC者Hibernate進行事務(wù)抽象。如果僅僅聚焦于web支持,Spring的一些顯著特點是清楚分離 :con troller,valid
18、ator,comma nd object,form object,model object,和DispatcherServlet,handler mapping vs view resolver等等強大而且直接的框架和作為JavaBeans的應(yīng)用配置,包括簡單的參照和應(yīng)用內(nèi)容,例如,從web控 制器到業(yè)務(wù)對象和數(shù)據(jù)校驗。適應(yīng)性 , 無論什么樣的控制器你都需要得到代替Action/ActionForm 所做的每件事情的方案 ( 簡單 , 命令 ,form, 范例 , 多重 action, 或 者定制一個) 重用業(yè)務(wù)邏輯代碼, 不需要復制 , 你可以用已經(jīng)有的業(yè)務(wù)邏輯對象作為命令或則 form 對
19、象代替反射特定的 ActionForm 子類??捎喼频慕壎ê蛿?shù)據(jù)校驗作為應(yīng)用級的輸入機制的錯誤校驗, 固定日期和數(shù)字綁定, 例如 , 通過手動 檢查和轉(zhuǎn)換業(yè)務(wù)對象來替換單個的 string ??捎喼频牟僮? 可訂制的視圖方案: 通過name/value Map 靈活的模型傳送, 用一個簡單的方法從簡單到復雜操作映射和視圖方案策略。 以在J2EE應(yīng)用中非常普遍的用戶登錄過程為例來說明上述體系結(jié)構(gòu)是如何具體運用的。登錄的流程非常清晰:用戶從登錄頁面 login.jsp 輸入登錄信息, 系統(tǒng)對 登錄信息進行驗證, 如果正確則成功登錄, 否則提示相應(yīng)錯誤信息。 在開 發(fā)過程 中,使用 Eclipse
20、做為開發(fā)環(huán)境,同時加載了對Struts 、 Spring 及 Hibernate 提供更好的控制和支持的第三方插件 MyEclipse , Web艮務(wù)器使用Tomcat,數(shù)據(jù)庫 選用了 Mysql 。首先對 Hibernate 進行配置,只需要對系統(tǒng)自動生成的 hibernate.cfg.xml 進行修改 , 配置好數(shù)據(jù)庫連接的各種參數(shù)以及定義數(shù)據(jù)映射文件。由于Hibernate 所帶的連接池主要用于測試,性能不是很好,可以通過JNDI 將其修改為使用 Tomcat 的連接池。A Development Frame Application Based on the Hibernate、the
21、Struts and the Spring J2EEOne preface along with the Java technique of gradual mature and perfect. Be establishment business enterprise class application of standardterrace,the J2 EE terrace got substantial of development .Several technique asked for help from to include in the J2 EEnorm : Enterpris
22、e JavaBean (EJB), Java Servlets (Servlet), Java Server Pages(JSP), Java MessageService(JMS)etc., development manyapplication system.But, also appeared some problem in the tradition J2 the EE the application of the development the process:1)the antinomy of of data model and logic model. Currently the
23、 database of usage basically and all is relation type database, but the Java be essentially a kind of the language which face to object, object at saving with read usage SQL and JDBC carry on a database operation and lowered plait distance of efficiency and system of can maintenance;2)tradition of J
24、2 EE application much the adoption is according to the EJBheavy weight frame, this kind of frame suitable for develop a large business enterprise application, but usage the EJB container carry on development and adjust to try to need to be waste a great deal of time.For lowering the Ou of code to ma
25、tch sex, exaltation system of development efficiency, this text put forward one kind EE according to the J2 of the Struts frame 、 the Hibernate frame and the Spring frame application development strategy.The Hibernate FrameDatas holding out for long time layer and Hibernate is one piece according to
26、 hold out for long time layer frame, is a kind of realization object and relation of the tool which reflect to shoot(O/R Mapping), it carried on the object of the lightweight to pack to the JDBC and make procedure member can usage object plait distance thought to operation database. It not only prov
27、ided to shoot from Java to reflect of data form, but also provided a data a search and instauration mechanism. Opposite in usage JDBC and SQL to operation database, use Hibernate ability consumedly of exaltation realization of efficiency. The Hibernate frame use allocation document of the form come
28、to the reflect of the definition Java object and data form to shoot relation, in the meantime at more deep of level of data form of relation explanation for the relations such as inherit of and containment etc. of Java object. Pass the usage HQLlanguage sentence complications of relation the calcula
29、te way use the way of object description, to a large extent simplification logarithms according to of search, speed development of efficiency. Have in the Hibernate a simple but keep the API of view, used for to the database mean of object performance search. Want to establish or the modification be
30、 these objects, need in the procedure carry on with them to hand over with each other, then tell Hibernate to keep. So, a great deal of pack hold out for long time turn operation of business logic no longer demand write a trivial JDBC language sentence, make data last long thus the layer got biggest
31、 of simplification.Use the Struts realization MVCstructure MVC(Model-View-Controller) is put forward by the Trygve Reenskaug, first drive application in the environment SmallTalk-80, is many to hand over with each other with interface system of constitute foundation. According to the need of variabl
32、e of the interface design, MVChand over with each other constitute of system to resolve into model and see diagram, controller three part. Model(Model) is software processing problem logic at independence in outside manifestation under contents and form circumstance of inside abstract, packed the co
33、re data, logic of problem and function of calculation relation, independence in concrete of interface expression and I/O operation. See diagram(View) meaninformation and particular formdemonstration of model data and logic relation and appearance to thecustomer. It acquire a manifestation informatio
34、n from the model, there can be many for homology of information dissimilarity of manifestation form or see diagram. The controller(Controller) is a processing the customer hand over with software with each other operation of, its job is control provide model in any variety of dissemination, insure a
35、 customer interface amongthe model of rightness should contact; It accept a customer of importation, give the importation feedback model, thenrealization compute model control, is make model and see diagram to moderate work of parts. Usually 1 see a diagram rightness should a controller. Model, see
36、separate of diagram and controller, make a model be able to have many manifestation to see diagram. If the customer pass a certain see the controller of diagram change the data of model, all other dependence in these see of data diagram all should reflection arrive these variety. When therefore and
37、regardless occurrence what data variety, controller all would variety notice ally see diagram, cause manifestation of renewal. This is actually a kind of variety of model-dissemination mechanism.The Struts FrameThe Struts frame is to be the item of Apache Jakarta to constitute part to publish luck t
38、o do at the earliest stage, it inherited MVCof each item characteristic, and did according to the characteristics of J2 EE correspond of variety with expand. The Struts frame was good to combine Jsp , Java Servlet , Java Bean, Talia etc. technique. In the Struts, what to undertake the controller rol
39、e in the MVC be an ActionServlet. The ActionServlet is an in general use control module. This control module provided a processing all HTTP claim which send out Struts of entrance point. Its interception with distribute these claim to arrive correspond of action type.(these action all of type is Act
40、ion son type)Moreover the control module is also responsible for using to correspond of claim the parameter fill Action Form(FromBean), and pass action type(ActionBean).Action type the business logic of the interview core, then interview Java Bean or adjust to use EJB. End action type control the po
41、wer pass follow-up of JSP document, from JSP document born see diagram. All these control logic make use of Struts-config.xml the document come to allocation. See diagram in the Struts frame main from JSP born page completion, the Struts provide abundant of JSP label database, this is advantageous t
42、o separating performance logic and procedure logic.The model is with 1 or the form existence of several Java Bean. In the Struts, main existence three kinds of Bean, respectively BE: Action, ActionForm, EJB perhaps Java Bean. The Struts frame have no concrete definition model layer of realization, i
43、n actually the development, model layer usually is close with business logic connect with each other, and want to carry on operation to the first floor data. The underneaths introduction is a kind of development strategy, lead the Hibernate into the model layer of Struts frame, usage it to carry on
44、a data to pack with reflect to shoot, provide hold out for long time turn of support. 4 usage Hibernate and the Struts development J2 EEapplication 4.1 system structure diagram 3 manifestation according to Hibernate and Struts development strategy of system structure diagram.the Spring FrameSpring i
45、snt particularly a web framework but a generic lightweight application framework with dedicated web support, and show the architectural differences to Struts and WebWorkIn contrast to Struts or WebWork,Spring is an application framework for all layers: It offers a bean configuration foundation, AOP
46、support, JDBCabstraction framework, abstract transaction support etc. It is a very non-intrusive effort: Your application classes do not need to depend on any Spring classes if not necessary, and you can reuse every part on its own if you like to. From its very design, the frameworkencourages cleanseparation of tiers, most importantly web tier and business logic. e.g: the validation framework does not depend on web controllers. Major goals are reusability and testability: Unnecessary container or
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025-2030全球元件參數(shù)測試儀行業(yè)調(diào)研及趨勢分析報告
- 2024年科普知識競賽試題庫及答案(共70題)
- 2024年青少年禁毒知識競賽小學組題庫及答案(共60題)
- 2025年度特種鋼材進口與國內(nèi)銷售合作協(xié)議
- 2025年度應(yīng)急響應(yīng)個人勞務(wù)派遣服務(wù)合同示范文本2篇
- 二零二五年度車庫租賃及停車場運營管理合同4篇
- 數(shù)字化背景下學校師德師風教育的創(chuàng)新發(fā)展
- 數(shù)學教育與兒童發(fā)展游戲化教學的意義
- 二零二五年度鋁扣板藝術(shù)裝飾施工合同3篇
- 二零二五年度采砂場環(huán)境保護與修復合同3篇
- JB-T 8532-2023 脈沖噴吹類袋式除塵器
- 深圳小學英語單詞表(中英文)
- 護理質(zhì)量反饋內(nèi)容
- 山東省濟寧市2023年中考數(shù)學試題(附真題答案)
- 抖音搜索用戶分析報告
- 板帶生產(chǎn)工藝熱連軋帶鋼生產(chǎn)
- 鉆孔灌注樁技術(shù)規(guī)范
- 2023-2024學年北師大版必修二unit 5 humans and nature lesson 3 Race to the pole 教學設(shè)計
- 供貨進度計劃
- 國際尿失禁咨詢委員會尿失禁問卷表
- 彌漫大B細胞淋巴瘤護理查房
評論
0/150
提交評論