Professional Documents
Culture Documents
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 的方式封裝,供開發人員重用。
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 的定位
Struts is one of your architectural decisions
Your Code
Web tier / Persistence /workflow
WebLogic / WebSphere ?
jRocket VM / JDK ?
Linux / Windows ?
How Struts fits into RUP ?
讓成 功的 設計經 驗可 以重複 使用 !
框架與應用程式的關係
框架可以看成是整個應用程式的骨架,程式開發
人員將框架客製化以寫出真正的應用程式。
框架 客製化 應用 程式
( 抽象的、不能使 ( 具體的、可
用) 以使用的 )
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 控制流程,它會呼叫你所提供的實作 !
你控 制流 程, Library 提供 實作 !
Structured Style!
Framework Reuse
public class MyServlet extends HttpServlet
{
public void doGet(…){
// 提供實作
}
}
Framework 控制流程 ,你 提供實 作 !
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 、 hook 在同一個 Class
Template
method
Hook
method
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) 。
實作界面就可保 符合規格
ps. 其實這是一個 strategy pattern
java.io.FilenameFilter
Separation Observer
Pattern
Template Hook
method method
ASP.NET 的標準做法
Model 1 : Page Controller
index.jsp
Java Bean
Request or
Session Scope
在 JSP 中決定下一頁是那裏,所
以稱為 Page Controller
Model 1 有什麼問題 ?
One controller per page , hard to maintain !
Web Server
MVC : Separation of Concern
M
Model 2 - MVC
index.htm ControllerServlet BO
Forward/Redirect
set data
index.jsp
read
data
Java Bean
Request or
Session Scope
Controller Split
index.htm Controller
LoginAction BO
index.jsp
Java Bean
Request or
Session Scope
Controller Split in Struts
Controller
View
Action Class Business
index.jsp Logic
ActionForm Model
Request or
Session Scope
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)
http://localhost:8080/jpetstore/index.do
index.do
index IndexAction
viewCategory ViewCategoryAction
Hans MVC
簡單的 MVC 實作
查表
Struts modifications
Abstract Action
struts-config.xml
ActionServlet
+
RequestProcessor
使用 ActionForm
來協助處理 HTML 表單
每一個 HTML Form 背後都有一個 ActionForm
來 support 。
<<ActionForm>>
LoginActionForm
id
id
password
password getId()/setId()
getPassword()
setPassword()
reset()
validate()
ActionForm 的處理
ActionSerlvet 會依據 config 檔
將使用者填入的資料填入 ActionForm 中
/processName.do
askName.jsp ActionServlet
<<ActionForm>>
UserNameForm
<<Action>>
ProcessNameAction
ActionForm 的會以參數的形式傳給 Action 類別,
供開發者在取得 ActionForm 的資料
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:……>
<taglib>
<taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
<taglib-location>
/WEB-INF/tld/struts-html.tld
</taglib-location>
</taglib>
TLD 真正的位置
隨便取個名字,識別用 .
使用 Struts 控制項
取代傳統 html 控制項
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<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>
ActionForm 名稱
</html:html>
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
ActionForm 的名字,將在
<form-bean ActionMapping 區段中使用
name=“userNameForm”
type=“demo.UserNameForm”
/>
ActionForm 類別名稱
其它屬性
className – 指定 FormBeanConfig 類別。
dynamic – 是否使用 DynaForm ?
使用 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 的屬
性
讀取 ActionForm 的資料
public class ProcessNameAction extends Action
{ 以參數形式傳入
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
取得 ActionForm 的參考
HttpServletRequest request,
HttpServletResponse response)
{
UserNameForm myForm = (UserNameForm) form;
…..(do something)…
return (mapping.findForward(“sayHello"));
}
}
Action Class
(revisited) Struts 的 MVC 模型
index.htm ActionServlet
Controller
View
Action Class Business
index.jsp Logic
ActionForm Model
Request or
Session Scope
Action 做些什麼 ?
1. 從 ActionForm 中取得資料
2. 從 request/response 中取得資料
3. 委任商業邏輯 ( 含資料庫存取 )
4. 將控制權 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"));
}
}
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> ActionMapping 區段
<form-bean 將使用這個名字來指稱
name="userNameForm" 你的 FormBeans
type="simple.form.SimpleForm“
/>
<form-bean
name=“anotherForm"
type="simple.form.AnotherForm“
/>
</form-beans>
ActionMappings 區段
目的 : 將 jsp 背後的 ActionForm 、發出的 url 、
Action class 及其處理完後要 forward 的對象,
四者間的關係連結起來。
ActionMappings 區段
如何將以下這幾個 component 的關係化成文字 ?
/sayHello.do
index.jsp ActionServlet
Request Scope
UserNameForm
hello.jsp SayHelloAction
success
ActionMappings 區段
<action-mappings> 要將 .do 去掉
<action path="/sayHello"
type="simple.action.SayHelloAction"
name="userNameForm"
scope="request"
input=“index.jsp” >
<forward name="success" path="/hello.jsp"/>
</action>
<action>…..</action>
<action>…..</action>
</action-mappings>
Struts Application Demo(1)
- 使用 JBuilder X
系統設計
/processName.do
askName.jsp ActionServlet
Request Scope
UserNameForm
sayHello.jsp ProcessNameAction
sayHello
輸入驗証 (Validation)
Client side validation
Server side validation
實作 ActionForm.validate()
Struts 的輸入驗証 架構
實作 validation 步驟
1. 在 ActionForm 中實作 validate 方法
2. struts-config.xml 中的 <action> 標籤中之
validate 屬性設為 true
3. 設定 ApplicationResource.properties
4. 寫作 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
try@nccu.edu.tw
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 族群的聖殿。