*******************************************************************************
Model
事務(wù)邏輯 -- 要做什么
Action類所扮演的角色 -- 決定做什么
內(nèi)部狀態(tài)通常由一組ActionForm JavaBean表示,通常我們建議將事務(wù)邏輯與Action分開
*******************************************************************************
View
JSP 標(biāo)簽
*******************************************************************************
Controller
基本的控制器組件是ActionServlet類中的實(shí)例servlet,實(shí)際使用的servlet在配置文件
中由一組映射(由ActionMapping類進(jìn)行描述)進(jìn)行定義。
如:
1 <action-mappings>
2 <action attribute="fStrutsForm" input="/form/fStruts.jsp"
3 name="fStrutsForm" path="/fStruts" scope="request"
4 type="com.nicky.struts.action.FStrutsAction" />
5 </action-mappings>
ActionServlet 控制器2 <action attribute="fStrutsForm" input="/form/fStruts.jsp"
3 name="fStrutsForm" path="/fStruts" scope="request"
4 type="com.nicky.struts.action.FStrutsAction" />
5 </action-mappings>
ActionClass 包含事務(wù)邏輯
ActionForm 表單數(shù)據(jù)
ActionMapping 將請(qǐng)求映射到操作
ActionForward 轉(zhuǎn)移對(duì)象
ActionError 存儲(chǔ)和回收錯(cuò)誤
ActionServlet將適當(dāng)?shù)腁ctionMapping轉(zhuǎn)發(fā)給適當(dāng)?shù)腁ction
*******************************************************************************
\WEB-INF\struts-config.xml 可以定義全局轉(zhuǎn)發(fā)、Action Mapping、ActionForm Bean、
JDBC數(shù)據(jù)源等
1 <controller></controller>
2 <action-mappings></action-mappings>
3 <data-sources></data-sources>
4 <form-beans></form-beans>
5 <global-exceptions></global-exceptions>
6 <global-forwards></global-forwards>
7 <message-resources parameter="com.nicky.struts.ApplicationResources"/>
8 <plug-in className=""></plug-in>
其中全局轉(zhuǎn)發(fā)是用來(lái)在jsp頁(yè)面之間創(chuàng)建邏輯名稱的映射;action-mappings進(jìn)行內(nèi)部的流程2 <action-mappings></action-mappings>
3 <data-sources></data-sources>
4 <form-beans></form-beans>
5 <global-exceptions></global-exceptions>
6 <global-forwards></global-forwards>
7 <message-resources parameter="com.nicky.struts.ApplicationResources"/>
8 <plug-in className=""></plug-in>
控制,可以將請(qǐng)求URL映射到Action類,并將Action類與ActionForm Bean相關(guān)聯(lián);data-sources
用來(lái)配置JDBC數(shù)據(jù)源,可以定義多個(gè)數(shù)據(jù)源,實(shí)例:
1 <data-sources>
2 <data-source>
3 <set-property property="key" value="mysqlDS" />
4 <set-property property="type" value="javax.sql.DataSource" />
5 <set-property property="driverClass"
6 value="com.mysql.jdbc.Driver" />
7 <set-property property="url"
8 value="jdbc:mysql://DBServer:3306/DBName" />
9 <set-property property="maxCount" value="20" />
10 <set-property property="minCount" value="2" />
11 <set-property property="user" value="userName" />
12 <set-property property="password" value="userPassword" />
13 </data-source>
14 </data-sources>
2 <data-source>
3 <set-property property="key" value="mysqlDS" />
4 <set-property property="type" value="javax.sql.DataSource" />
5 <set-property property="driverClass"
6 value="com.mysql.jdbc.Driver" />
7 <set-property property="url"
8 value="jdbc:mysql://DBServer:3306/DBName" />
9 <set-property property="maxCount" value="20" />
10 <set-property property="minCount" value="2" />
11 <set-property property="user" value="userName" />
12 <set-property property="password" value="userPassword" />
13 </data-source>
14 </data-sources>
在Action中調(diào)用:
//MySQL 數(shù)據(jù)源取得
DataSource mysqlds = this.getDataSource(request,"mysqlDS");
對(duì)于單數(shù)據(jù)源 可以直接獲取
DataSource mysqlds = this.getDataSource(request);
*******************************************************************************
Action類的perform過(guò)時(shí),替以execute也是兩個(gè)可以處理Http和非Http請(qǐng)求。
*******************************************************************************
使用ActionError和ActionErrors來(lái)處理錯(cuò)誤,ActionErrors保存著ActionError對(duì)象集合,
而每個(gè)ActionError都包含關(guān)鍵字,能夠映射到資源文件中存儲(chǔ)的錯(cuò)誤信息。
資源文件是在ActionServlet初始花參數(shù)中指定的。
替換例子:
1 error.invalid=<b>Invalid Number{0}</b>
2 ActionError error = new ActionError(‘error.invalid’,new Double(-1));
JSP頁(yè)顯示:Invalild Number –12 ActionError error = new ActionError(‘error.invalid’,new Double(-1));
*******************************************************************************
struts——>bean、logic、html、template、nested、tiles
<bean:define>定義字符串常數(shù),拷貝bean對(duì)象,用現(xiàn)有bean屬性來(lái)創(chuàng)建新bean。id新定義bean腳本變量名稱,type類,value為id屬性定義的腳本變量分配一個(gè)新的對(duì)象,name目標(biāo)bean的名稱。
1 <bean:define id="printString" value="This is Login Page" scope="page"></bean:define>
2 <bean:define id="printString" name="_printString" scope="page" toScope="request" ></bean:define>
<bean:include>標(biāo)記將對(duì)一個(gè)資源的響應(yīng)進(jìn)行檢索,并引入一個(gè)腳本變量和字符串類型的頁(yè)作用域?qū)傩裕Y源可以是一個(gè)頁(yè),一個(gè)ActionForward或一個(gè)外部URL2 <bean:define id="printString" name="_printString" scope="page" toScope="request" ></bean:define>
<bean:resource>標(biāo)記將檢索web應(yīng)用中的資源,并引入一個(gè)腳本變量和InputStream或字符串類型的頁(yè)作用域?qū)傩?br /> <bean:write>標(biāo)記,用來(lái)將bean的屬性輸送到封裝的JSP頁(yè)寫入器
struts框架對(duì)國(guó)際化的支持是使用<bean:message>標(biāo)記,以及使用java.util數(shù)據(jù)包中定義的Locale和ResourceBundle類來(lái)實(shí)現(xiàn)Java2平臺(tái)對(duì)這些任務(wù)的支持。
為了實(shí)現(xiàn)國(guó)際化,所有的資源文件必須都存儲(chǔ)在基本資源文件所在的目錄中。基本資源文件包含的是用默認(rèn)地區(qū)語(yǔ)言-本地語(yǔ)言編寫的消息。如果基本資源文件的名稱是ApplicationResources.properties,那么用其他特定語(yǔ)言編寫的資源文件的名稱就應(yīng)該是ApplicationResources_xx.properties(xx為ISO編碼,如英語(yǔ)是en)。因此這些文件應(yīng)包含相同的關(guān)鍵字,但關(guān)鍵字的值是用特定語(yǔ)言編寫的。
在邏輯標(biāo)記庫(kù)中定義了<logic:iterate>標(biāo)記,它能夠根據(jù)特定集合中元素的數(shù)目對(duì)標(biāo)記體的內(nèi)容進(jìn)行重復(fù)的檢查
<logic:forward>標(biāo)記能夠?qū)㈨憫?yīng)轉(zhuǎn)發(fā)給重定向到特定的全局ActionForward上。
<logic:redirect>標(biāo)記是一個(gè)能夠執(zhí)行HTTP重定向的強(qiáng)大工具
Onblur字段失去了焦點(diǎn)
Struts HTML標(biāo)記可以大致地分為以下幾個(gè)功能:顯示表單元素和輸入控件 顯示錯(cuò)誤信息 顯示其他HTML元素
1 <html:form action="/fStruts">
2 用戶名 : <html:text property="userName" size="33"/><html:errors property="userName"/><br/>
3 密 碼 : <html:password property="pwdText" size = "35"/><html:errors property="pwdText"/><br/>
4 <html:submit/><html:cancel/>
5 </html:form>
動(dòng)態(tài)模板是模塊化WEB頁(yè)布局設(shè)計(jì)的強(qiáng)大手段。Struts模板標(biāo)記庫(kù)定義了自定義標(biāo)記來(lái)實(shí)現(xiàn)動(dòng)態(tài)模板2 用戶名 : <html:text property="userName" size="33"/><html:errors property="userName"/><br/>
3 密 碼 : <html:password property="pwdText" size = "35"/><html:errors property="pwdText"/><br/>
4 <html:submit/><html:cancel/>
5 </html:form>
<template:insert>標(biāo)記能夠在應(yīng)用程序的JSP頁(yè)中插入動(dòng)態(tài)模板。這個(gè)標(biāo)記只有一個(gè)template屬性,用來(lái)定義模板JSP頁(yè)。要插入到模板的頁(yè)是有多個(gè)<template:put>標(biāo)記來(lái)指定的,而這些標(biāo)記被定義為<template:insert>標(biāo)記的主體內(nèi)容
在模板JSP頁(yè)中使用<template:get>標(biāo)記能夠檢索由<template:put>標(biāo)記插入到JSP頁(yè)的資源
*******************************************************************************