SlideShare a Scribd company logo
J2EE 前端應用程式架構與 Struts Framework 廖峻鋒 NCCU Computer Center Feb 2,2003
Agenda Struts 簡介與定位 Web-tier 概觀 Framework & Design Patterns 概論 基本觀念與常用 OO 技巧簡介 Struts Framework Architectural Overview The  MVC  Architectural Pattern Struts 設計理念 Struts  實作議題
What is Struts? 基於 MVC  Architectural Pattern 所開發的  Web  Application Framework   Craig McClanahan  在  2000 年提出, 2001 年 6 月 release struts 1.0 。 Hosted in  Apache Jakarta project 將開發 web 程式之 best practice 以 Application Framework 的方式封裝,供開發人員重用。 為何有資格稱為 best practice ?
Must Read when  Architecting J2EE Applications P of EAA Core J2EE Patterns 2/e
Struts 與 J2EE Core Patterns Struts 實作了下列 J2EE Core Patterns Application Controller (PEAA) Front Controller (PEAA) View Helper (PEAA:Template View) Service to Worker Dispatcher View
Struts Framework 的定位 Web tier / Persistence /workflow Your Code WebLogic / WebSphere ? jRocket VM / JDK ? Linux / Windows ? Struts is one of your   architectural decisions
How Struts fits into RUP ? Architecture should be finished
J2EE 前端的任務 UI ( 顯示畫面 ) Presentation Logic ( 顯示邏輯 ) Collects Inputs  Page Flow Control 決定接下來要顯示那個 Page 當不使用 EJB 時… May  Maintain State (HttpSession) May  Implement Business Logic  J2EE  前端  = Web Tier
J2EE 前端的相關規格  /  技術 JSP / Servlets 最新版為 2.0 / 2.4 , 目前廣泛使用 1.2/2.3 JSP Custom Tag Library JSTL (JSP Standard Tag Library) 目前為 1.0 (2003.6) JavaServer™ Faces  目前為 1.0 final draft (2003.12.19) UI components / UI Event Handling
問題與討論 試以簡圖表示 JSP 與 Servlet 如何運作 ? 什麼是 Container? 什麼是 Servlet Container? 至少說出三個 JSP 與 ASP 不同之處 ? 一個完全使用 Servlet 的應用程式可能有什麼缺點 ? 什麼是 scriptlet ?  一個完全使用 scriptlet  的 JSP 應用程式可能有什麼缺點 ? JSP 1.2  規格書中訂有那四個 scope ,其意義為何 ? 使用時機為何 ? 請舉例說明一個合乎 Servlet 2.3 規格書的 Web Application 應該長什麼樣子。 Forward 和 Redirect 有何不同 ?
Framework and Design Patterns Struts  Framework  is built upon several  well-known proven  patterns 簡介
如果建築師都用軟體工程師的方法來建造房子,那麼第一隻飛來的啄木鳥便足以將整個文明都給摧毀。 - Applied Java Patterns by Stephen Stelting and Olava Maassen
Design Patterns Christopher Alexander (U.C. Berkeley) – 1977 出版了一本建築學用的樣式目錄 (Pattern- Catalog) 。 1987 OOPSLA  中 Kent Beck 等人發表了 smalltalk 的 Design Patterns 。 1995 年  Gof 出版了 Design patterns:Elements of Reusable Object-Oriented Software 。 1996 年  Frank 等人出版了 POSA(Pattern Oriented Software Architecture) 。 這二本書問世後, Design Patterns  開始受到廣泛的重視與成長。
Two primary books POSA Gof
Pattern System 由 [POSA96] 首先提出對 Patterns 的分類 將 Pattern 從「鉅觀」到「微觀」加以分類 Architectural Patterns ( 例如 :MVC 、 Layer) Design Patterns ( 例如 :Gof 的 23 個 patterns) Idioms ( 例如大量接合字串問題或 Code Style)
框架 (Framework) A framework is a  reusable, semi-complete  application( 半成品 ) that can be specialized to produce custom applications. Application frameworks are for  reifying( 具體化 ) proven software designs and implementations . Framework 通常針對特定領域來設計。 讓成功的設計經驗可以重複使用 !
框架與應用程式的關係 框架可以看成是整個應用程式的 骨架 ,程式開發人員將框架 客製化 以寫出真正的應用程式。 框架 ( 抽象的、不能使用 ) 應用程式 ( 具體的、可以使用的 ) 客製化
Framework 與 Patterns 的比較 Design Patterns 是從很多 Framework 中歸納得來。 一個 Framework 可能會包括很多 Design Patterns 。  Design Patterns 只能在觀念層級被重用。 Patterns are  more abstract than  frameworks Framework 通常會由一群 Design Patterns 組成。
使用 Framework  的好處 ? 讓成功的經驗得以重複使用。 將領域專家 (Domain Expert) 的知識封裝於 Framework 中。 鼓勵重用。 增進開發效率。 將軟體開發經驗轉換成具體資產。
Applying Framework Framework Develop time  2002.11 ~ 2003.06 (7 個月 / 1 人  ) Applications Develop time 改寫時間 2003.7 ~ 2003.8 (2 個月 / 2 人  )
使用 Framework 的經驗 Developers should have strong OO backgrounds. 在 Programmer 沒有足夠 OO Background 的情況下套用 Framework 可能造成反效果 . Learning curve should be taken into account. Once framework is on-line , it will be very hard to modified.
Framework and Design Patterns 幾個常用到的基本技巧
實作 和 流程控制 角色互換 ( Inversion of Control) 傳統的公用函式庫 (Library) 函式由函式庫定義。 我們寫主程式,在主程式中呼叫函式。 你控制流程 , Library 提供實作。 框架 (Framework) 主程式由框架定義。 我們實作函式, 被框架定義的主程式所呼叫 。 Framework 控制流程,它會呼叫 你所提供的實作 ! 又稱 :HollyWood 法則   Don’t call me, I will call you!
Inversion of Control Library 使用者的實作 Framework 使用者的實作
Library Reuse String countStr = “256”; String count =  Integer.parseInt (countStr); 你控制流程, Library 提供實作 ! Structured Style!
Framework Reuse public class MyServlet  extends HttpServlet { public void doGet(…){ //  提供實作 } } Framework 控制流程,你提供實作 ! Call Back Style!
建構框架基本原則 -Essential Framework construction principles Unification Separation
Template Method /Hook Method Framework 的作者通常將 主要邏輯 寫在 Template Method  中。 Template Method 會呼叫若干 Hook Method , Hook Method 通常就是「變異點 (Hot Spot) 」。 覆寫 Hook Method ,就可改變 Template Method 的行為。 (  例如 Servlet 的 doGet()/doPost() ) 參考下頁的例子。
範例 :LoginHandler
Unification Template method Hook method template 、 hook 在同一個 Class
Servlet  與 Template Method [Gof95] 一個 Browser 向 HttpServlet 發出 post 時,會觸發 doPost() 方法。 一個 Browser 向 Servlet 發出 get 時,會觸發 doGet() 方法。 判別是 get  或  post ,由 HttpServlet 決定,至於 doPost() , doGet() 由子類別決定。 Template Method 是 Framework 中最常見到的 Design Pattern!
Servlet
Interface Interface 規定了一組契約 (method) ,所有實作它的類別都要實作 所有方法 。 Client 呼叫的是 Interface 中的方法,所以元件的抽換對 Client 來說是感覺不到的 ( 不用改 code) 。
實作界面就可保証符合規格
java.io.FilenameFilter ps. 其實這是一個 strategy pattern
Separation Template method Hook method Template Class  與  Hook Class 有委任關係 Observer Pattern
範例 : 時常需要切換多台資料庫 template method hook method 在 template method 中呼叫 hook method
討論 : 彈性的登入機制 Which is template method ? which is hook method ?
Struts Architectural Overview
伺服端應用程式設計的演進 Servlet JSP scriptlet JSP+Bean (Model 1, Page Controller)  MVC (Model 2)
Model 1 在 JSP 中決定下一頁是那裏,所以稱為 Page Controller ,通常 Page Controller 會有多個。 (PEAA) ASP.NET 的標準做法
Model 1 : Page Controller login.jsp 資料庫 index.htm index.jsp 在 JSP 中決定下一頁是那裏,所以稱為 Page Controller Java Bean Request or Session Scope
Model 1 有什麼問題  ? Servlet/ JSP Servlet/ JSP Servlet/ JSP Web Server Servlet/ JSP One controller per page , hard to maintain !
MVC : Separation of Concern C V M
Model 2 - MVC index.jsp BO index.htm ControllerServlet Java Bean read data set data Request or Session Scope Forward/Redirect
Controller Split index.jsp LoginAction BO Request or Session Scope index.htm Java Bean Controller
Controller Split in Struts Controller = ActionServlet + Action Classes
MVC index.jsp LoginAction BO Request or Session Scope index.htm Java Bean Model View Controller Controller
Struts 的 MVC 模型 index.jsp Action Class Business Logic Request or Session Scope index.htm ActionForm Model View Controller ActionServlet
Strengths of MVC Pattern Provides a clear separation between: Business Logic (M)  Output Presentation (V) Request Processing (C) Provides single point of  workflow control Increases code manageability Increases code extensibility
MVC Implementations Hans MVC  Struts Framework
MVC 實作基本觀念 (1) index . do Action String Controller String Controller String 用來指定 Controller 類別來處理 Action String 用來指定要交給那個 Action 類別處理 http://localhost:8080/jpetstore/ index.do
MVC 實作基本觀念 (2) Action Table 在 Controller 中以 HashMap 方式實作 Controller 經由查表可得知該使用那個那個 Action 類別來處理該 Action String 。 ViewCategoryAction viewCategory IndexAction index Action 類別 Action String
Hans MVC
簡單的 MVC 實作 查表
Struts modifications struts-config.xml ActionServlet + RequestProcessor Abstract Action
 
使用 ActionForm 來協助處理 HTML 表單 每一個 HTML Form 背後都有一個 ActionForm 來 support 。 id password <<ActionForm>> LoginActionForm id password getId()/setId() getPassword() setPassword() reset() validate()
ActionForm 的處理 askName.jsp ProcessNameAction ActionServlet /processName.do UserNameForm ActionSerlvet 會依據 config 檔 將使用者填入的資料填入 ActionForm 中 ActionForm 的會以參數的形式傳給 Action 類別, 供開發者在取得 ActionForm 的資料 <<ActionForm>> <<Action>>
struuts-config.xml 主要區段 依先後次序為 : DataSource 設定     (JDBC) ActionForm 設定    重要 ! Global 相關設定 ActionMapping 設定    重要 ! Controller 設定 其它設定 (i18n,plug-in,resource…)
struts-config.xml ActionForm  設定 ActionMapping  設定
struts-config.xml 的處理
Author: Jean-Michel Garnier , http://rollerjm.free.fr/pro/Struts11.html
Developing Web Applications with Struts Framework
Struts 主要元件 Struts JSP 自訂標籤庫 ActionForm Action classes ActionServlet struts-config.xml
Struts - View
JSP 的寫作 : 二種選擇 JSTL <c:out value=“$ {sessionScope.userBean.name }” /> Struts-bean <bean:write name=“ userBean ” property=“ name ” />
JSTL 與 Struts Tag ,何者優先 ? Should you use JSTL tags instead of Struts tags whenever you can ? Sure, if your container supports Servlets 2.3 and JSP 1.2, and  that’s what  you  want to do .( 儘可能使用 JSTL 取代 Struts 標籤 ) If JSTL already existed, most of the Struts tags would neverhave been written. -Ted Husted ,Jakarta Struts technical lead. JSTL 優先 !
JSTL 與 Struts tag 取捨準則 ( 摘自 Struts in Action by Ted Husted) 使用 JSTL 取代 <bean:…> 與 <logic:…> 繼續使用 <html:……> 目前 Struts 正在開發 Struts-EL ,未來在 <html:…> 中將可使用 Expression Language .
使用 Struts 控制項 取代傳統 Html 控制項 目的 : 使用 Struts 的 html 控制項才能利用 ActionForm 的好處。 有時會和 JSTL 一起合併使用。
傳統的 Html Form <html> <body> <form action=“sayHello.do”> <input type=“text” name=“userName”/> <input type=“submit” /> </form> </body> </html>
使用 Struts 控制項 取代 html 控制項 修改 web.xml 副檔名改成 .jsp 修改 tag
修改 web.xml <taglib> <taglib-uri>/WEB-INF/struts-html.tld</taglib-uri> <taglib-location> /WEB-INF/tld/struts-html.tld </taglib-location> </taglib> 使用 Struts 控制項 取代傳統 html 控制項 隨便取個名字,識別用 . TLD 真正的位置
使用 Struts 控制項 取代傳統 html 控制項 <%@ taglib uri=&quot;/WEB-INF/struts-html.tld&quot; prefix=&quot;html&quot; %> <html:html> <html:form action=“sayHello.do”> <html:text property=“userName” />  <html:submit /> </html:form> </html:html> 要先宣告 前面加上 html 的 prefix 相同 !
HTML 的 Form 如何與 ActionForm 對映 ? 基本上每一個 <html:form>……</html:form> 背後都會有一個 ActionForm 。 表單的每一個輸入欄位原則上對映到 JavaBean 的一個 property 。
範例 <html:html> <html:form action=“sayHello.do”> <html:text  name=“userNameForm”   property=“userName”  />  <html:text  name=“userNameForm”   property=“password”  /> <html:submit /> </html:form> </html:html> ActionForm 名稱 所對映到的屬性
UserNameForm Package demo; Public class UserNameForm  extends ActionForm {  //  屬性和 html form 的要一一對應,名字也要相同 private String userName; private String password; //  每一個屬性都要有 getter 及 setter public void setUserName(String userName) {…} public String getUserName() {…..} public void setPassword(){…..} public String getPassword()  {….} } 如果欄位很多,寫起來很麻煩 ?
在那裏登記 ActionForm? ActionForm  設定
登記 ActionForm <form-bean  name=“userNameForm”  type=“demo.UserNameForm”  /> 其它屬性 className  –  指定 FormBeanConfig 類別。 dynamic  –  是否使用 DynaForm ? ActionForm 類別名稱 ActionForm 的名字,將在 ActionMapping 區段中使用
使用 DynaBean <form-bean  name=“userNameForm”  type=“ org.apache.struts.action.DynaActionForm ”   dynamic=“ true ”  > <form-property name=“userName” type=“java.lang.String”/> <form-property name=“password” type=“java.lang.String”/> </form-bean> 使用單一 Map 物件儲存所有 JavaBean 的屬性 登記屬性名稱
public class ProcessNameAction extends Action { public ActionForward execute( ActionMapping mapping, ActionForm form , HttpServletRequest request, HttpServletResponse response) { UserNameForm myForm = (UserNameForm) form; … ..(do something)… return (mapping.findForward(“sayHello&quot;)); } } 讀取 ActionForm 的資料 以參數形式傳入 取得 ActionForm 的參考
Action Class
(revisited) Struts 的 MVC 模型 index.jsp Action Class Business Logic Request or Session Scope index.htm ActionForm Model View Controller ActionServlet
Action 做些什麼 ? 從 ActionForm 中取得資料 從 request/response 中取得資料 委任商業邏輯 ( 含資料庫存取 ) 將控制權 forward 到合適的 View 中
ProcessNameAction public class ProcessNameAction  extends Action { public  ActionForward  execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { UserNameForm  myForm  = (UserNameForm) form; … ..(do something)… return (mapping.findForward(sayHello&quot;)); } }
ActionServlet 使用 struts-config.xml 控制 ActionServlet 行為 !
Struts-config.xml Struts 的核心,用來描述各元件的關係。 市面上有很多 GUI 工具可支援自動產生。
struuts-config.xml 主要區段 依先後次序為 : DataSource 設定     (JDBC) ActionForm 設定    重要 ! Global 相關設定 ActionMapping 設定    重要 ! Controller 設定 其它設定 (i18n,plug-in,…) 我們只講 ActionForm 及 ActionMapping ,其它 config 的細節請自行參考 struts 線上文件。
ActionForm 區段 目的 : 在此宣告你寫的 ActionForm 及其代名 ( 你要怎麼稱呼他 ?) ,以便在 ActionMapping 中做相對設定。 以 <form-beans>…</form-beans> 為界。 <form-beans> 中包含數個 <form-bean> 。
ActionForm 區段 <form-beans> <form-bean  name=&quot;userNameForm&quot;  type=&quot;simple.form.SimpleForm“ /> <form-bean  name=“anotherForm&quot;  type=&quot;simple.form.AnotherForm“ /> </form-beans> ActionMapping 區段將使用這個名字來指稱你的 FormBeans
ActionMappings 區段 目的 : 將 jsp 背後的 ActionForm 、發出的 url  、 Action class 及其處理完後要 forward 的對象,四者間的關係連結起來。
ActionMappings 區段 如何將以下這幾個 component 的關係化成文字 ? index.jsp SayHelloAction hello.jsp ActionServlet /sayHello.do success Request Scope UserNameForm
ActionMappings 區段 <action-mappings> <action  path =&quot;/sayHello&quot; type =&quot;simple.action.SayHelloAction&quot; name =&quot;userNameForm&quot; scope =&quot;request&quot;  input =“index.jsp” > <forward name=&quot;success&quot;  path=&quot;/hello.jsp&quot;/> </action> <action>…..</action> <action>…..</action> </action-mappings> 要將 .do 去掉
Struts Application Demo(1) - 使用 JBuilder X
系統設計 askName.jsp ProcessNameAction sayHello.jsp ActionServlet /processName.do sayHello Request Scope UserNameForm
輸入驗証 (Validation) Client side validation Server side validation 實作 ActionForm.validate()
Struts  的輸入驗証架構
實作 validation 步驟 在 ActionForm 中實作 validate 方法 struts-config.xml 中的 <action> 標籤中之 validate 屬性設為 true 設定 ApplicationResource.properties 寫作 ActionError 與錯誤訊息
Struts Application Demo(2) -validation
Struts 就這樣嗎 ? 還有很多有趣的細節等待你發掘。 Tiles – Template framework 更多 Default Action Classes ProcessBean Validating Framework Exception Handling I18n 建議參考書藉 –  Struts in Action by T.Husted et al.
其它注意事項
Q & A [email_address] http://java.cc.nccu.edu.tw
backup
Request Dispatching /sales /report ? month=Jan Servlet Context Context Path Query String
Framework 進階閱讀 Building Application Framework (Wiley)- 這本很像論文集,不好唸且錯誤很多,宜以讀書會方式加以了解。 Java Application Framework (Wiley). 應用架構入門與實例 – 台灣本土唯一一本 Framework  著作,但是是數年前的著作,前幾章仍有閱讀的價值。 ( 可自 MISOO 上自由下傳 ) 。
Design Patterns 學習地圖 精通任一種 OO 語言及 OO 基本觀念 ( 繼承封裝多形 ) 。 Thinking in Java( 有中譯本 ) . Design Patterns 入門 Java 與樣式理論  / Java 與樣式實作  ( 閻宏 ) Applying UML and Patterns: An Introduction to Object-Oriented Analysis, 2/e( 有中譯本 )  Design Patterns Explained: A New Perspective on Object-Oriented Design( 有中譯本 )  Design Patterns  於  Java  語言上的實習應用 ( 有中譯本 )
Design Patterns 學習地圖 2 觀察別人如何使用 Patterns(Java) Java Design Patterns: A Tutorial Applied Java Patterns( 有中譯本 )  Patterns in Java VI,V2. 活用 Patterns Design Patterns Java Workbook( 有中譯本 )  Pattern Hatching: Design Patterns Applied( 這本是以 C++ 為例 ).
Design Patterns 學習地圖 3 經典 Gof( 有中譯本 )  Architectural Patterns Patterns of Enterprise Application Architecture Pattern Oriented Software Architecture (POSA) 這本書 scope 非常廣,包含了 Software Architecture 、 Architectural Patterns  、 Design Patterns 及 Idioms 。
Design Patterns 學習地圖 4 Enterprise Design Patterns Patterns in Java V3 POSA2 Core J2EE Patterns / 2e P of EAA HillSide Group( http://hillside.net ) -  由 Kent Beck  及 Grady Booch 建立,是 Pattern 族群的聖殿。
Ad

More Related Content

What's hot (20)

API Survey #2 - Firebase realtime database
API Survey #2 - Firebase realtime databaseAPI Survey #2 - Firebase realtime database
API Survey #2 - Firebase realtime database
Szuping Wang
 
J2ee经典学习笔记
J2ee经典学习笔记J2ee经典学习笔记
J2ee经典学习笔记
yiditushe
 
Spring 2.0 技術手冊第六章 - Hibernate 與 Spring
Spring 2.0 技術手冊第六章 - Hibernate 與 SpringSpring 2.0 技術手冊第六章 - Hibernate 與 Spring
Spring 2.0 技術手冊第六章 - Hibernate 與 Spring
Justin Lin
 
Jni文档
Jni文档Jni文档
Jni文档
woshiwzy
 
ASP.NET MVC 內建驗證擴充與活用技巧 -twMVC#3
ASP.NET MVC 內建驗證擴充與活用技巧 -twMVC#3ASP.NET MVC 內建驗證擴充與活用技巧 -twMVC#3
ASP.NET MVC 內建驗證擴充與活用技巧 -twMVC#3
twMVC
 
Powercli
PowercliPowercli
Powercli
hellocn
 
Spring 2.0 技術手冊第一章 - 認識 Spring
Spring 2.0 技術手冊第一章 - 認識 SpringSpring 2.0 技術手冊第一章 - 認識 Spring
Spring 2.0 技術手冊第一章 - 認識 Spring
Justin Lin
 
ASP.NET MVC Code Templates實戰開發 -twMVC#4
 ASP.NET MVC Code Templates實戰開發 -twMVC#4 ASP.NET MVC Code Templates實戰開發 -twMVC#4
ASP.NET MVC Code Templates實戰開發 -twMVC#4
twMVC
 
TypeScript-twmvc#16
TypeScript-twmvc#16TypeScript-twmvc#16
TypeScript-twmvc#16
twMVC
 
IKVM.NET 深入敵營的 Java
IKVM.NET 深入敵營的 JavaIKVM.NET 深入敵營的 Java
IKVM.NET 深入敵營的 Java
建興 王
 
Asp.net mvc 概觀介紹
Asp.net mvc 概觀介紹Asp.net mvc 概觀介紹
Asp.net mvc 概觀介紹
Alan Tsai
 
Entities in DCPS (DDS)
Entities in DCPS (DDS)Entities in DCPS (DDS)
Entities in DCPS (DDS)
Jamie (Taka) Wang
 
Java Api Design
Java Api DesignJava Api Design
Java Api Design
yiditushe
 
Java 1(Java概述)
Java 1(Java概述)Java 1(Java概述)
Java 1(Java概述)
xzdbd
 
Spring 2.0 技術手冊第四章 - Spring AOP
Spring 2.0 技術手冊第四章 - Spring AOPSpring 2.0 技術手冊第四章 - Spring AOP
Spring 2.0 技術手冊第四章 - Spring AOP
Justin Lin
 
AngularJS 開發 ASP.NET MVC -twMVC#9
AngularJS 開發 ASP.NET MVC -twMVC#9AngularJS 開發 ASP.NET MVC -twMVC#9
AngularJS 開發 ASP.NET MVC -twMVC#9
twMVC
 
beidakejian
beidakejianbeidakejian
beidakejian
hanxuebing
 
API Survey #2 - Firebase realtime database
API Survey #2 - Firebase realtime databaseAPI Survey #2 - Firebase realtime database
API Survey #2 - Firebase realtime database
Szuping Wang
 
J2ee经典学习笔记
J2ee经典学习笔记J2ee经典学习笔记
J2ee经典学习笔记
yiditushe
 
Spring 2.0 技術手冊第六章 - Hibernate 與 Spring
Spring 2.0 技術手冊第六章 - Hibernate 與 SpringSpring 2.0 技術手冊第六章 - Hibernate 與 Spring
Spring 2.0 技術手冊第六章 - Hibernate 與 Spring
Justin Lin
 
ASP.NET MVC 內建驗證擴充與活用技巧 -twMVC#3
ASP.NET MVC 內建驗證擴充與活用技巧 -twMVC#3ASP.NET MVC 內建驗證擴充與活用技巧 -twMVC#3
ASP.NET MVC 內建驗證擴充與活用技巧 -twMVC#3
twMVC
 
Powercli
PowercliPowercli
Powercli
hellocn
 
Spring 2.0 技術手冊第一章 - 認識 Spring
Spring 2.0 技術手冊第一章 - 認識 SpringSpring 2.0 技術手冊第一章 - 認識 Spring
Spring 2.0 技術手冊第一章 - 認識 Spring
Justin Lin
 
ASP.NET MVC Code Templates實戰開發 -twMVC#4
 ASP.NET MVC Code Templates實戰開發 -twMVC#4 ASP.NET MVC Code Templates實戰開發 -twMVC#4
ASP.NET MVC Code Templates實戰開發 -twMVC#4
twMVC
 
TypeScript-twmvc#16
TypeScript-twmvc#16TypeScript-twmvc#16
TypeScript-twmvc#16
twMVC
 
IKVM.NET 深入敵營的 Java
IKVM.NET 深入敵營的 JavaIKVM.NET 深入敵營的 Java
IKVM.NET 深入敵營的 Java
建興 王
 
Asp.net mvc 概觀介紹
Asp.net mvc 概觀介紹Asp.net mvc 概觀介紹
Asp.net mvc 概觀介紹
Alan Tsai
 
Java Api Design
Java Api DesignJava Api Design
Java Api Design
yiditushe
 
Java 1(Java概述)
Java 1(Java概述)Java 1(Java概述)
Java 1(Java概述)
xzdbd
 
Spring 2.0 技術手冊第四章 - Spring AOP
Spring 2.0 技術手冊第四章 - Spring AOPSpring 2.0 技術手冊第四章 - Spring AOP
Spring 2.0 技術手冊第四章 - Spring AOP
Justin Lin
 
AngularJS 開發 ASP.NET MVC -twMVC#9
AngularJS 開發 ASP.NET MVC -twMVC#9AngularJS 開發 ASP.NET MVC -twMVC#9
AngularJS 開發 ASP.NET MVC -twMVC#9
twMVC
 

Similar to Struts Mitac(1) (20)

Asp.net mvc 從無到有 -twMVC#2
Asp.net mvc 從無到有 -twMVC#2Asp.net mvc 從無到有 -twMVC#2
Asp.net mvc 從無到有 -twMVC#2
twMVC
 
twMVC#02 | ASP.NET MVC 從無到有
twMVC#02 | ASP.NET MVC 從無到有twMVC#02 | ASP.NET MVC 從無到有
twMVC#02 | ASP.NET MVC 從無到有
twMVC
 
通过Struts构建Web应用
通过Struts构建Web应用通过Struts构建Web应用
通过Struts构建Web应用
yiditushe
 
Real World ASP.NET MVC
Real World ASP.NET MVCReal World ASP.NET MVC
Real World ASP.NET MVC
jeffz
 
網站設計100步
網站設計100步網站設計100步
網站設計100步
evercislide
 
Struts+Spring+Hibernate整合教程
Struts+Spring+Hibernate整合教程Struts+Spring+Hibernate整合教程
Struts+Spring+Hibernate整合教程
appollo0312
 
Struts+Spring+Hibernate整合教程
Struts+Spring+Hibernate整合教程Struts+Spring+Hibernate整合教程
Struts+Spring+Hibernate整合教程
yiditushe
 
Servlet & JSP 教學手冊第二版試讀 - 撰寫與設定 Servlet
Servlet & JSP 教學手冊第二版試讀 - 撰寫與設定 ServletServlet & JSP 教學手冊第二版試讀 - 撰寫與設定 Servlet
Servlet & JSP 教學手冊第二版試讀 - 撰寫與設定 Servlet
Justin Lin
 
Mvc training
Mvc trainingMvc training
Mvc training
xioxu
 
Visual studio 2012 與 asp.net 4.5 (新功能與開發介紹) 第一天
Visual studio 2012 與 asp.net 4.5 (新功能與開發介紹) 第一天Visual studio 2012 與 asp.net 4.5 (新功能與開發介紹) 第一天
Visual studio 2012 與 asp.net 4.5 (新功能與開發介紹) 第一天
Gelis Wu
 
Single-Page Application Design Principles 101
Single-Page Application Design Principles 101Single-Page Application Design Principles 101
Single-Page Application Design Principles 101
Jollen Chen
 
淘宝网前端开发面试题
淘宝网前端开发面试题 淘宝网前端开发面试题
淘宝网前端开发面试题
Lumend
 
基于J2 Ee 的通用Web 信息系统框架设计与实现
基于J2 Ee 的通用Web 信息系统框架设计与实现基于J2 Ee 的通用Web 信息系统框架设计与实现
基于J2 Ee 的通用Web 信息系统框架设计与实现
yiditushe
 
twMVC#04 | ASP.NET MVC 4 新功能介紹(快速上手)
twMVC#04 | ASP.NET MVC 4 新功能介紹(快速上手)twMVC#04 | ASP.NET MVC 4 新功能介紹(快速上手)
twMVC#04 | ASP.NET MVC 4 新功能介紹(快速上手)
twMVC
 
Terracotta And Continuent Based Clustering Architecture
Terracotta And Continuent Based Clustering ArchitectureTerracotta And Continuent Based Clustering Architecture
Terracotta And Continuent Based Clustering Architecture
Target Source
 
ASP.NET Core MVC 2.2從開發到測試 - Development & Unit Testing
ASP.NET Core MVC 2.2從開發到測試 - Development & Unit TestingASP.NET Core MVC 2.2從開發到測試 - Development & Unit Testing
ASP.NET Core MVC 2.2從開發到測試 - Development & Unit Testing
江華 奚
 
Django敏捷开发 刘天斯
Django敏捷开发 刘天斯Django敏捷开发 刘天斯
Django敏捷开发 刘天斯
liuts
 
ASP.NET MVC (Gi Days)
ASP.NET MVC (Gi Days)ASP.NET MVC (Gi Days)
ASP.NET MVC (Gi Days)
leeju lee
 
JdonFramework中文
JdonFramework中文JdonFramework中文
JdonFramework中文
banq jdon
 
Angular.js & ASP.NET in Study4
Angular.js & ASP.NET in Study4Angular.js & ASP.NET in Study4
Angular.js & ASP.NET in Study4
Kyle Shen
 
Asp.net mvc 從無到有 -twMVC#2
Asp.net mvc 從無到有 -twMVC#2Asp.net mvc 從無到有 -twMVC#2
Asp.net mvc 從無到有 -twMVC#2
twMVC
 
twMVC#02 | ASP.NET MVC 從無到有
twMVC#02 | ASP.NET MVC 從無到有twMVC#02 | ASP.NET MVC 從無到有
twMVC#02 | ASP.NET MVC 從無到有
twMVC
 
通过Struts构建Web应用
通过Struts构建Web应用通过Struts构建Web应用
通过Struts构建Web应用
yiditushe
 
Real World ASP.NET MVC
Real World ASP.NET MVCReal World ASP.NET MVC
Real World ASP.NET MVC
jeffz
 
網站設計100步
網站設計100步網站設計100步
網站設計100步
evercislide
 
Struts+Spring+Hibernate整合教程
Struts+Spring+Hibernate整合教程Struts+Spring+Hibernate整合教程
Struts+Spring+Hibernate整合教程
appollo0312
 
Struts+Spring+Hibernate整合教程
Struts+Spring+Hibernate整合教程Struts+Spring+Hibernate整合教程
Struts+Spring+Hibernate整合教程
yiditushe
 
Servlet & JSP 教學手冊第二版試讀 - 撰寫與設定 Servlet
Servlet & JSP 教學手冊第二版試讀 - 撰寫與設定 ServletServlet & JSP 教學手冊第二版試讀 - 撰寫與設定 Servlet
Servlet & JSP 教學手冊第二版試讀 - 撰寫與設定 Servlet
Justin Lin
 
Mvc training
Mvc trainingMvc training
Mvc training
xioxu
 
Visual studio 2012 與 asp.net 4.5 (新功能與開發介紹) 第一天
Visual studio 2012 與 asp.net 4.5 (新功能與開發介紹) 第一天Visual studio 2012 與 asp.net 4.5 (新功能與開發介紹) 第一天
Visual studio 2012 與 asp.net 4.5 (新功能與開發介紹) 第一天
Gelis Wu
 
Single-Page Application Design Principles 101
Single-Page Application Design Principles 101Single-Page Application Design Principles 101
Single-Page Application Design Principles 101
Jollen Chen
 
淘宝网前端开发面试题
淘宝网前端开发面试题 淘宝网前端开发面试题
淘宝网前端开发面试题
Lumend
 
基于J2 Ee 的通用Web 信息系统框架设计与实现
基于J2 Ee 的通用Web 信息系统框架设计与实现基于J2 Ee 的通用Web 信息系统框架设计与实现
基于J2 Ee 的通用Web 信息系统框架设计与实现
yiditushe
 
twMVC#04 | ASP.NET MVC 4 新功能介紹(快速上手)
twMVC#04 | ASP.NET MVC 4 新功能介紹(快速上手)twMVC#04 | ASP.NET MVC 4 新功能介紹(快速上手)
twMVC#04 | ASP.NET MVC 4 新功能介紹(快速上手)
twMVC
 
Terracotta And Continuent Based Clustering Architecture
Terracotta And Continuent Based Clustering ArchitectureTerracotta And Continuent Based Clustering Architecture
Terracotta And Continuent Based Clustering Architecture
Target Source
 
ASP.NET Core MVC 2.2從開發到測試 - Development & Unit Testing
ASP.NET Core MVC 2.2從開發到測試 - Development & Unit TestingASP.NET Core MVC 2.2從開發到測試 - Development & Unit Testing
ASP.NET Core MVC 2.2從開發到測試 - Development & Unit Testing
江華 奚
 
Django敏捷开发 刘天斯
Django敏捷开发 刘天斯Django敏捷开发 刘天斯
Django敏捷开发 刘天斯
liuts
 
ASP.NET MVC (Gi Days)
ASP.NET MVC (Gi Days)ASP.NET MVC (Gi Days)
ASP.NET MVC (Gi Days)
leeju lee
 
JdonFramework中文
JdonFramework中文JdonFramework中文
JdonFramework中文
banq jdon
 
Angular.js & ASP.NET in Study4
Angular.js & ASP.NET in Study4Angular.js & ASP.NET in Study4
Angular.js & ASP.NET in Study4
Kyle Shen
 
Ad

More from wangjiaz (20)

We9 Struts 2.0
We9 Struts 2.0We9 Struts 2.0
We9 Struts 2.0
wangjiaz
 
Workshop Sutdio3(1)
Workshop Sutdio3(1)Workshop Sutdio3(1)
Workshop Sutdio3(1)
wangjiaz
 
Ye Ying(1)
Ye Ying(1)Ye Ying(1)
Ye Ying(1)
wangjiaz
 
Zhanbin Web2.0
Zhanbin Web2.0Zhanbin Web2.0
Zhanbin Web2.0
wangjiaz
 
U Co M Protocol
U Co M ProtocolU Co M Protocol
U Co M Protocol
wangjiaz
 
Tt Seminar Taipei 9605(1)
Tt Seminar Taipei 9605(1)Tt Seminar Taipei 9605(1)
Tt Seminar Taipei 9605(1)
wangjiaz
 
The New Iphone
The New IphoneThe New Iphone
The New Iphone
wangjiaz
 
Sxdylgn(1)
Sxdylgn(1)Sxdylgn(1)
Sxdylgn(1)
wangjiaz
 
Struts Intro Course(1)
Struts Intro Course(1)Struts Intro Course(1)
Struts Intro Course(1)
wangjiaz
 
S11flash(1)
S11flash(1)S11flash(1)
S11flash(1)
wangjiaz
 
Role Play Exercises
Role Play ExercisesRole Play Exercises
Role Play Exercises
wangjiaz
 
Recruit(1)
Recruit(1)Recruit(1)
Recruit(1)
wangjiaz
 
web 2.0 class notes
web 2.0 class notesweb 2.0 class notes
web 2.0 class notes
wangjiaz
 
Ye Ying(1)
Ye Ying(1)Ye Ying(1)
Ye Ying(1)
wangjiaz
 
Xzzx20060302 1
Xzzx20060302 1Xzzx20060302 1
Xzzx20060302 1
wangjiaz
 
We9 Struts 2.0
We9 Struts 2.0We9 Struts 2.0
We9 Struts 2.0
wangjiaz
 
Workshop Sutdio3(1)
Workshop Sutdio3(1)Workshop Sutdio3(1)
Workshop Sutdio3(1)
wangjiaz
 
Ye Ying(1)
Ye Ying(1)Ye Ying(1)
Ye Ying(1)
wangjiaz
 
Zhanbin Web2.0
Zhanbin Web2.0Zhanbin Web2.0
Zhanbin Web2.0
wangjiaz
 
U Co M Protocol
U Co M ProtocolU Co M Protocol
U Co M Protocol
wangjiaz
 
Tt Seminar Taipei 9605(1)
Tt Seminar Taipei 9605(1)Tt Seminar Taipei 9605(1)
Tt Seminar Taipei 9605(1)
wangjiaz
 
The New Iphone
The New IphoneThe New Iphone
The New Iphone
wangjiaz
 
Sxdylgn(1)
Sxdylgn(1)Sxdylgn(1)
Sxdylgn(1)
wangjiaz
 
Struts Intro Course(1)
Struts Intro Course(1)Struts Intro Course(1)
Struts Intro Course(1)
wangjiaz
 
S11flash(1)
S11flash(1)S11flash(1)
S11flash(1)
wangjiaz
 
Role Play Exercises
Role Play ExercisesRole Play Exercises
Role Play Exercises
wangjiaz
 
Recruit(1)
Recruit(1)Recruit(1)
Recruit(1)
wangjiaz
 
web 2.0 class notes
web 2.0 class notesweb 2.0 class notes
web 2.0 class notes
wangjiaz
 
Ye Ying(1)
Ye Ying(1)Ye Ying(1)
Ye Ying(1)
wangjiaz
 
Xzzx20060302 1
Xzzx20060302 1Xzzx20060302 1
Xzzx20060302 1
wangjiaz
 
Ad

Struts Mitac(1)

  • 1. J2EE 前端應用程式架構與 Struts Framework 廖峻鋒 NCCU Computer Center Feb 2,2003
  • 2. Agenda Struts 簡介與定位 Web-tier 概觀 Framework & Design Patterns 概論 基本觀念與常用 OO 技巧簡介 Struts Framework Architectural Overview The MVC Architectural Pattern Struts 設計理念 Struts 實作議題
  • 3. What is Struts? 基於 MVC Architectural Pattern 所開發的 Web Application Framework Craig McClanahan 在 2000 年提出, 2001 年 6 月 release struts 1.0 。 Hosted in Apache Jakarta project 將開發 web 程式之 best practice 以 Application Framework 的方式封裝,供開發人員重用。 為何有資格稱為 best practice ?
  • 4. Must Read when Architecting J2EE Applications P of EAA Core J2EE Patterns 2/e
  • 5. Struts 與 J2EE Core Patterns Struts 實作了下列 J2EE Core Patterns Application Controller (PEAA) Front Controller (PEAA) View Helper (PEAA:Template View) Service to Worker Dispatcher View
  • 6. Struts Framework 的定位 Web tier / Persistence /workflow Your Code WebLogic / WebSphere ? jRocket VM / JDK ? Linux / Windows ? Struts is one of your architectural decisions
  • 7. How Struts fits into RUP ? Architecture should be finished
  • 8. J2EE 前端的任務 UI ( 顯示畫面 ) Presentation Logic ( 顯示邏輯 ) Collects Inputs Page Flow Control 決定接下來要顯示那個 Page 當不使用 EJB 時… May Maintain State (HttpSession) May Implement Business Logic J2EE 前端 = Web Tier
  • 9. J2EE 前端的相關規格 / 技術 JSP / Servlets 最新版為 2.0 / 2.4 , 目前廣泛使用 1.2/2.3 JSP Custom Tag Library JSTL (JSP Standard Tag Library) 目前為 1.0 (2003.6) JavaServer™ Faces 目前為 1.0 final draft (2003.12.19) UI components / UI Event Handling
  • 10. 問題與討論 試以簡圖表示 JSP 與 Servlet 如何運作 ? 什麼是 Container? 什麼是 Servlet Container? 至少說出三個 JSP 與 ASP 不同之處 ? 一個完全使用 Servlet 的應用程式可能有什麼缺點 ? 什麼是 scriptlet ? 一個完全使用 scriptlet 的 JSP 應用程式可能有什麼缺點 ? JSP 1.2 規格書中訂有那四個 scope ,其意義為何 ? 使用時機為何 ? 請舉例說明一個合乎 Servlet 2.3 規格書的 Web Application 應該長什麼樣子。 Forward 和 Redirect 有何不同 ?
  • 11. Framework and Design Patterns Struts Framework is built upon several well-known proven patterns 簡介
  • 13. Design Patterns Christopher Alexander (U.C. Berkeley) – 1977 出版了一本建築學用的樣式目錄 (Pattern- Catalog) 。 1987 OOPSLA 中 Kent Beck 等人發表了 smalltalk 的 Design Patterns 。 1995 年 Gof 出版了 Design patterns:Elements of Reusable Object-Oriented Software 。 1996 年 Frank 等人出版了 POSA(Pattern Oriented Software Architecture) 。 這二本書問世後, Design Patterns 開始受到廣泛的重視與成長。
  • 14. Two primary books POSA Gof
  • 15. Pattern System 由 [POSA96] 首先提出對 Patterns 的分類 將 Pattern 從「鉅觀」到「微觀」加以分類 Architectural Patterns ( 例如 :MVC 、 Layer) Design Patterns ( 例如 :Gof 的 23 個 patterns) Idioms ( 例如大量接合字串問題或 Code Style)
  • 16. 框架 (Framework) A framework is a reusable, semi-complete application( 半成品 ) that can be specialized to produce custom applications. Application frameworks are for reifying( 具體化 ) proven software designs and implementations . Framework 通常針對特定領域來設計。 讓成功的設計經驗可以重複使用 !
  • 17. 框架與應用程式的關係 框架可以看成是整個應用程式的 骨架 ,程式開發人員將框架 客製化 以寫出真正的應用程式。 框架 ( 抽象的、不能使用 ) 應用程式 ( 具體的、可以使用的 ) 客製化
  • 18. Framework 與 Patterns 的比較 Design Patterns 是從很多 Framework 中歸納得來。 一個 Framework 可能會包括很多 Design Patterns 。 Design Patterns 只能在觀念層級被重用。 Patterns are more abstract than frameworks Framework 通常會由一群 Design Patterns 組成。
  • 19. 使用 Framework 的好處 ? 讓成功的經驗得以重複使用。 將領域專家 (Domain Expert) 的知識封裝於 Framework 中。 鼓勵重用。 增進開發效率。 將軟體開發經驗轉換成具體資產。
  • 20. Applying Framework Framework Develop time 2002.11 ~ 2003.06 (7 個月 / 1 人 ) Applications Develop time 改寫時間 2003.7 ~ 2003.8 (2 個月 / 2 人 )
  • 21. 使用 Framework 的經驗 Developers should have strong OO backgrounds. 在 Programmer 沒有足夠 OO Background 的情況下套用 Framework 可能造成反效果 . Learning curve should be taken into account. Once framework is on-line , it will be very hard to modified.
  • 22. Framework and Design Patterns 幾個常用到的基本技巧
  • 23. 實作 和 流程控制 角色互換 ( Inversion of Control) 傳統的公用函式庫 (Library) 函式由函式庫定義。 我們寫主程式,在主程式中呼叫函式。 你控制流程 , Library 提供實作。 框架 (Framework) 主程式由框架定義。 我們實作函式, 被框架定義的主程式所呼叫 。 Framework 控制流程,它會呼叫 你所提供的實作 ! 又稱 :HollyWood 法則  Don’t call me, I will call you!
  • 24. Inversion of Control Library 使用者的實作 Framework 使用者的實作
  • 25. Library Reuse String countStr = “256”; String count = Integer.parseInt (countStr); 你控制流程, Library 提供實作 ! Structured Style!
  • 26. Framework Reuse public class MyServlet extends HttpServlet { public void doGet(…){ // 提供實作 } } Framework 控制流程,你提供實作 ! Call Back Style!
  • 27. 建構框架基本原則 -Essential Framework construction principles Unification Separation
  • 28. Template Method /Hook Method Framework 的作者通常將 主要邏輯 寫在 Template Method 中。 Template Method 會呼叫若干 Hook Method , Hook Method 通常就是「變異點 (Hot Spot) 」。 覆寫 Hook Method ,就可改變 Template Method 的行為。 ( 例如 Servlet 的 doGet()/doPost() ) 參考下頁的例子。
  • 30. Unification Template method Hook method template 、 hook 在同一個 Class
  • 31. Servlet 與 Template Method [Gof95] 一個 Browser 向 HttpServlet 發出 post 時,會觸發 doPost() 方法。 一個 Browser 向 Servlet 發出 get 時,會觸發 doGet() 方法。 判別是 get 或 post ,由 HttpServlet 決定,至於 doPost() , doGet() 由子類別決定。 Template Method 是 Framework 中最常見到的 Design Pattern!
  • 33. Interface Interface 規定了一組契約 (method) ,所有實作它的類別都要實作 所有方法 。 Client 呼叫的是 Interface 中的方法,所以元件的抽換對 Client 來說是感覺不到的 ( 不用改 code) 。
  • 36. Separation Template method Hook method Template Class 與 Hook Class 有委任關係 Observer Pattern
  • 37. 範例 : 時常需要切換多台資料庫 template method hook method 在 template method 中呼叫 hook method
  • 38. 討論 : 彈性的登入機制 Which is template method ? which is hook method ?
  • 40. 伺服端應用程式設計的演進 Servlet JSP scriptlet JSP+Bean (Model 1, Page Controller) MVC (Model 2)
  • 41. Model 1 在 JSP 中決定下一頁是那裏,所以稱為 Page Controller ,通常 Page Controller 會有多個。 (PEAA) ASP.NET 的標準做法
  • 42. Model 1 : Page Controller login.jsp 資料庫 index.htm index.jsp 在 JSP 中決定下一頁是那裏,所以稱為 Page Controller Java Bean Request or Session Scope
  • 43. Model 1 有什麼問題 ? Servlet/ JSP Servlet/ JSP Servlet/ JSP Web Server Servlet/ JSP One controller per page , hard to maintain !
  • 44. MVC : Separation of Concern C V M
  • 45. Model 2 - MVC index.jsp BO index.htm ControllerServlet Java Bean read data set data Request or Session Scope Forward/Redirect
  • 46. Controller Split index.jsp LoginAction BO Request or Session Scope index.htm Java Bean Controller
  • 47. Controller Split in Struts Controller = ActionServlet + Action Classes
  • 48. MVC index.jsp LoginAction BO Request or Session Scope index.htm Java Bean Model View Controller Controller
  • 49. Struts 的 MVC 模型 index.jsp Action Class Business Logic Request or Session Scope index.htm ActionForm Model View Controller ActionServlet
  • 50. Strengths of MVC Pattern Provides a clear separation between: Business Logic (M) Output Presentation (V) Request Processing (C) Provides single point of workflow control Increases code manageability Increases code extensibility
  • 51. MVC Implementations Hans MVC Struts Framework
  • 52. MVC 實作基本觀念 (1) index . do Action String Controller String Controller String 用來指定 Controller 類別來處理 Action String 用來指定要交給那個 Action 類別處理 http://localhost:8080/jpetstore/ index.do
  • 53. MVC 實作基本觀念 (2) Action Table 在 Controller 中以 HashMap 方式實作 Controller 經由查表可得知該使用那個那個 Action 類別來處理該 Action String 。 ViewCategoryAction viewCategory IndexAction index Action 類別 Action String
  • 56. Struts modifications struts-config.xml ActionServlet + RequestProcessor Abstract Action
  • 57.  
  • 58. 使用 ActionForm 來協助處理 HTML 表單 每一個 HTML Form 背後都有一個 ActionForm 來 support 。 id password <<ActionForm>> LoginActionForm id password getId()/setId() getPassword() setPassword() reset() validate()
  • 59. ActionForm 的處理 askName.jsp ProcessNameAction ActionServlet /processName.do UserNameForm ActionSerlvet 會依據 config 檔 將使用者填入的資料填入 ActionForm 中 ActionForm 的會以參數的形式傳給 Action 類別, 供開發者在取得 ActionForm 的資料 <<ActionForm>> <<Action>>
  • 60. struuts-config.xml 主要區段 依先後次序為 : DataSource 設定  (JDBC) ActionForm 設定  重要 ! Global 相關設定 ActionMapping 設定  重要 ! Controller 設定 其它設定 (i18n,plug-in,resource…)
  • 61. struts-config.xml ActionForm 設定 ActionMapping 設定
  • 63. Author: Jean-Michel Garnier , http://rollerjm.free.fr/pro/Struts11.html
  • 64. Developing Web Applications with Struts Framework
  • 65. Struts 主要元件 Struts JSP 自訂標籤庫 ActionForm Action classes ActionServlet struts-config.xml
  • 67. JSP 的寫作 : 二種選擇 JSTL <c:out value=“$ {sessionScope.userBean.name }” /> Struts-bean <bean:write name=“ userBean ” property=“ name ” />
  • 68. JSTL 與 Struts Tag ,何者優先 ? Should you use JSTL tags instead of Struts tags whenever you can ? Sure, if your container supports Servlets 2.3 and JSP 1.2, and that’s what you want to do .( 儘可能使用 JSTL 取代 Struts 標籤 ) If JSTL already existed, most of the Struts tags would neverhave been written. -Ted Husted ,Jakarta Struts technical lead. JSTL 優先 !
  • 69. JSTL 與 Struts tag 取捨準則 ( 摘自 Struts in Action by Ted Husted) 使用 JSTL 取代 <bean:…> 與 <logic:…> 繼續使用 <html:……> 目前 Struts 正在開發 Struts-EL ,未來在 <html:…> 中將可使用 Expression Language .
  • 70. 使用 Struts 控制項 取代傳統 Html 控制項 目的 : 使用 Struts 的 html 控制項才能利用 ActionForm 的好處。 有時會和 JSTL 一起合併使用。
  • 71. 傳統的 Html Form <html> <body> <form action=“sayHello.do”> <input type=“text” name=“userName”/> <input type=“submit” /> </form> </body> </html>
  • 72. 使用 Struts 控制項 取代 html 控制項 修改 web.xml 副檔名改成 .jsp 修改 tag
  • 73. 修改 web.xml <taglib> <taglib-uri>/WEB-INF/struts-html.tld</taglib-uri> <taglib-location> /WEB-INF/tld/struts-html.tld </taglib-location> </taglib> 使用 Struts 控制項 取代傳統 html 控制項 隨便取個名字,識別用 . TLD 真正的位置
  • 74. 使用 Struts 控制項 取代傳統 html 控制項 <%@ taglib uri=&quot;/WEB-INF/struts-html.tld&quot; prefix=&quot;html&quot; %> <html:html> <html:form action=“sayHello.do”> <html:text property=“userName” /> <html:submit /> </html:form> </html:html> 要先宣告 前面加上 html 的 prefix 相同 !
  • 75. HTML 的 Form 如何與 ActionForm 對映 ? 基本上每一個 <html:form>……</html:form> 背後都會有一個 ActionForm 。 表單的每一個輸入欄位原則上對映到 JavaBean 的一個 property 。
  • 76. 範例 <html:html> <html:form action=“sayHello.do”> <html:text name=“userNameForm” property=“userName” /> <html:text name=“userNameForm” property=“password” /> <html:submit /> </html:form> </html:html> ActionForm 名稱 所對映到的屬性
  • 77. UserNameForm Package demo; Public class UserNameForm extends ActionForm { // 屬性和 html form 的要一一對應,名字也要相同 private String userName; private String password; // 每一個屬性都要有 getter 及 setter public void setUserName(String userName) {…} public String getUserName() {…..} public void setPassword(){…..} public String getPassword() {….} } 如果欄位很多,寫起來很麻煩 ?
  • 79. 登記 ActionForm <form-bean name=“userNameForm” type=“demo.UserNameForm” /> 其它屬性 className – 指定 FormBeanConfig 類別。 dynamic – 是否使用 DynaForm ? ActionForm 類別名稱 ActionForm 的名字,將在 ActionMapping 區段中使用
  • 80. 使用 DynaBean <form-bean name=“userNameForm” type=“ org.apache.struts.action.DynaActionForm ” dynamic=“ true ” > <form-property name=“userName” type=“java.lang.String”/> <form-property name=“password” type=“java.lang.String”/> </form-bean> 使用單一 Map 物件儲存所有 JavaBean 的屬性 登記屬性名稱
  • 81. public class ProcessNameAction extends Action { public ActionForward execute( ActionMapping mapping, ActionForm form , HttpServletRequest request, HttpServletResponse response) { UserNameForm myForm = (UserNameForm) form; … ..(do something)… return (mapping.findForward(“sayHello&quot;)); } } 讀取 ActionForm 的資料 以參數形式傳入 取得 ActionForm 的參考
  • 83. (revisited) Struts 的 MVC 模型 index.jsp Action Class Business Logic Request or Session Scope index.htm ActionForm Model View Controller ActionServlet
  • 84. Action 做些什麼 ? 從 ActionForm 中取得資料 從 request/response 中取得資料 委任商業邏輯 ( 含資料庫存取 ) 將控制權 forward 到合適的 View 中
  • 85. ProcessNameAction public class ProcessNameAction extends Action { public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { UserNameForm myForm = (UserNameForm) form; … ..(do something)… return (mapping.findForward(sayHello&quot;)); } }
  • 86. ActionServlet 使用 struts-config.xml 控制 ActionServlet 行為 !
  • 87. Struts-config.xml Struts 的核心,用來描述各元件的關係。 市面上有很多 GUI 工具可支援自動產生。
  • 88. struuts-config.xml 主要區段 依先後次序為 : DataSource 設定  (JDBC) ActionForm 設定  重要 ! Global 相關設定 ActionMapping 設定  重要 ! Controller 設定 其它設定 (i18n,plug-in,…) 我們只講 ActionForm 及 ActionMapping ,其它 config 的細節請自行參考 struts 線上文件。
  • 89. ActionForm 區段 目的 : 在此宣告你寫的 ActionForm 及其代名 ( 你要怎麼稱呼他 ?) ,以便在 ActionMapping 中做相對設定。 以 <form-beans>…</form-beans> 為界。 <form-beans> 中包含數個 <form-bean> 。
  • 90. ActionForm 區段 <form-beans> <form-bean name=&quot;userNameForm&quot; type=&quot;simple.form.SimpleForm“ /> <form-bean name=“anotherForm&quot; type=&quot;simple.form.AnotherForm“ /> </form-beans> ActionMapping 區段將使用這個名字來指稱你的 FormBeans
  • 91. ActionMappings 區段 目的 : 將 jsp 背後的 ActionForm 、發出的 url 、 Action class 及其處理完後要 forward 的對象,四者間的關係連結起來。
  • 92. ActionMappings 區段 如何將以下這幾個 component 的關係化成文字 ? index.jsp SayHelloAction hello.jsp ActionServlet /sayHello.do success Request Scope UserNameForm
  • 93. ActionMappings 區段 <action-mappings> <action path =&quot;/sayHello&quot; type =&quot;simple.action.SayHelloAction&quot; name =&quot;userNameForm&quot; scope =&quot;request&quot; input =“index.jsp” > <forward name=&quot;success&quot; path=&quot;/hello.jsp&quot;/> </action> <action>…..</action> <action>…..</action> </action-mappings> 要將 .do 去掉
  • 94. Struts Application Demo(1) - 使用 JBuilder X
  • 95. 系統設計 askName.jsp ProcessNameAction sayHello.jsp ActionServlet /processName.do sayHello Request Scope UserNameForm
  • 96. 輸入驗証 (Validation) Client side validation Server side validation 實作 ActionForm.validate()
  • 98. 實作 validation 步驟 在 ActionForm 中實作 validate 方法 struts-config.xml 中的 <action> 標籤中之 validate 屬性設為 true 設定 ApplicationResource.properties 寫作 ActionError 與錯誤訊息
  • 100. Struts 就這樣嗎 ? 還有很多有趣的細節等待你發掘。 Tiles – Template framework 更多 Default Action Classes ProcessBean Validating Framework Exception Handling I18n 建議參考書藉 – Struts in Action by T.Husted et al.
  • 102. Q & A [email_address] http://java.cc.nccu.edu.tw
  • 103. backup
  • 104. Request Dispatching /sales /report ? month=Jan Servlet Context Context Path Query String
  • 105. Framework 進階閱讀 Building Application Framework (Wiley)- 這本很像論文集,不好唸且錯誤很多,宜以讀書會方式加以了解。 Java Application Framework (Wiley). 應用架構入門與實例 – 台灣本土唯一一本 Framework 著作,但是是數年前的著作,前幾章仍有閱讀的價值。 ( 可自 MISOO 上自由下傳 ) 。
  • 106. Design Patterns 學習地圖 精通任一種 OO 語言及 OO 基本觀念 ( 繼承封裝多形 ) 。 Thinking in Java( 有中譯本 ) . Design Patterns 入門 Java 與樣式理論 / Java 與樣式實作 ( 閻宏 ) Applying UML and Patterns: An Introduction to Object-Oriented Analysis, 2/e( 有中譯本 ) Design Patterns Explained: A New Perspective on Object-Oriented Design( 有中譯本 ) Design Patterns 於 Java 語言上的實習應用 ( 有中譯本 )
  • 107. Design Patterns 學習地圖 2 觀察別人如何使用 Patterns(Java) Java Design Patterns: A Tutorial Applied Java Patterns( 有中譯本 ) Patterns in Java VI,V2. 活用 Patterns Design Patterns Java Workbook( 有中譯本 ) Pattern Hatching: Design Patterns Applied( 這本是以 C++ 為例 ).
  • 108. Design Patterns 學習地圖 3 經典 Gof( 有中譯本 ) Architectural Patterns Patterns of Enterprise Application Architecture Pattern Oriented Software Architecture (POSA) 這本書 scope 非常廣,包含了 Software Architecture 、 Architectural Patterns 、 Design Patterns 及 Idioms 。
  • 109. Design Patterns 學習地圖 4 Enterprise Design Patterns Patterns in Java V3 POSA2 Core J2EE Patterns / 2e P of EAA HillSide Group( http://hillside.net ) - 由 Kent Beck 及 Grady Booch 建立,是 Pattern 族群的聖殿。

Editor's Notes

  • #2: Understanding the struts framework Framework 是什麼 ? Struts Framework 的功用 ? 一定要用 Struts 嗎 ? 有沒有其它替代方案 ? Struts 要怎麼用 ?