Action
1、Action什么時(shí)候初始化?發(fā)出該Action請求,不是在讀取配置時(shí)初始化。
2、每個(gè)Action只會(huì)初始化一次。
3、Action是線程不安全的,因?yàn)樗姓埱蠊蚕硪粋€(gè)Action實(shí)例。
4、怎么實(shí)現(xiàn)Action的安全性編程?
a) 注意不用用實(shí)例變量或者類變量共享只是針對某個(gè)請求的數(shù)據(jù)。
b) 注意資源操作符的同步性。
c) 應(yīng)用:
i. 統(tǒng)計(jì)一個(gè)action訪問次數(shù)。
ii. 設(shè)計(jì)一個(gè)實(shí)例變量count在調(diào)用時(shí)加一。
ActionMapping
1、一個(gè)ActionMapping代表控制器的信息,這個(gè)ActionMapping實(shí)例用于選擇指定的Action傳遞到該Action。
2、ActionMapping提供了三個(gè)方法。
a) findForward(String forwardName) 根據(jù)傳入的ActionForward名字返回一個(gè)ActionForward。
b) findForwards() 返回本地mapping映射的所有邏輯名稱。
c) getInputForward() 創(chuàng)建返回一個(gè)ActionForward
ActionForward
1、ActionForward對象是配置對象,這些配置對象擁有獨(dú)一無二的標(biāo)識,允許他們按照name屬性來檢索。ActionForward對象封裝了前進(jìn)的URL路徑,且被請求處理器識別目標(biāo)視圖。
a) Name:邏輯名稱。
b) Path:頁面或者模塊訪問路徑。
c) Redirect:設(shè)置成true則使用HttpServletResponse.sendRedirect()容器內(nèi)跳轉(zhuǎn)到相關(guān)path,寫絕對地址。false為容器外跳轉(zhuǎn)RequestDispatcher.forward()路徑相對當(dāng)前應(yīng)用。
ActionForm
工作原理
處理ActionForm的一般步驟:
1、檢查Action的映射,確定Action中已經(jīng)配置了對ActionForm的映射。
2、根據(jù)name屬性,查找form bean的配置信息。
3、檢查Action的form bean的使用范圍,確定在此范圍下(request,session),是否已經(jīng)有此form bean的實(shí)例。
4、假如當(dāng)前范圍下,已經(jīng)存在了此form bean的實(shí)例,而是對當(dāng)前請求來說,是同一種類型的話,那么就重用。
5、否則就重新構(gòu)建一個(gè)form bean的實(shí)例(調(diào)用構(gòu)造方法),并且保存在一定的作用范圍。
6、Form bean的reset()方法調(diào)用。
7、調(diào)用對應(yīng)的setter方法,對狀態(tài)屬性賦值。
8、如果validate的屬性設(shè)置為true,那么就調(diào)用form bean的validate()方法。
9、如果validate()方法沒有返回任何錯(cuò)誤,控制器將ActionForm作為參數(shù),傳給Action實(shí)例的execute()方法并執(zhí)行。
注意:直接從ActionForm類繼承的reset()和validate()方法,并不能實(shí)現(xiàn)什么處理功能,所以有必要自己重新覆蓋。
注意:
1、無參構(gòu)造方法必須有。
2、Scope缺省值是session。
3、調(diào)用setter方法,實(shí)質(zhì)上是調(diào)用對應(yīng)的標(biāo)準(zhǔn)set方法,比如username,調(diào)用setUsername。
分析:
Scope.setAttribute();
1、從過程去查看
a) HttpSessionAttributeListener
b) ServletRequestAttributeListener
2、從結(jié)果去查看
Action:
1、attribute:用來存取form的關(guān)鍵字,缺省值與name一樣。
2、Validate是用于控制是否校驗(yàn)表單(校驗(yàn)開關(guān)),true(默認(rèn))~校驗(yàn),false~不校驗(yàn)。
全局跳轉(zhuǎn):
Action A ~ Error.jsp
Action B ~ Error.jsp
Action C ~ Error.jsp
Action ABC ~ Error.jsp
自定版本struts
開發(fā)以下類:
1、ActionServlet
a) 讀取配置dom4j
b) 填充form
c) 派發(fā)請求:調(diào)用對應(yīng)的Action的execute方法
d) 查找響應(yīng):
2、ActionForm
a) Reset
b) validate
3、Action
a) Execute(ActionMapping , ActionForm , HttpServletRequest , HttpServletResponse)
4、ActionMapping
a) Path
b) Name
c) Type
d) Validate
e) HashMap : forwards
5、ActionForward
a) Name
b) Path
6、配置文件
<struts-config>
<display>wudi版權(quán)所有,不得用于商業(yè)用途</display>
<form-beans>
<form-bean name="" type=""></form-bean>
</form-beans>
<action-mappings>
<action path="" type="" name="">
<forward name="" path=""/>
<forward name="" path=""/>
</action>
<action path="" type="" name="">
<forward name="" path=""/>
<forward name="" path=""/>
</action>
</action-mappings>
</struts-config>
7、ActionConfig,FormConfig
模仿struts填充form功能
分析:
n 創(chuàng)建AddStudentForm對象 ~ 根據(jù)類的全名進(jìn)行反射(Class.forName)
n 需要把頁面的所有參數(shù)名提取出來(request.getParameterNames ~ Enumeration)
n 把參數(shù)名與bean中的屬性名進(jìn)行匹配:
u 如果能匹配就把取出來的值填充到bean中(借助BeanUtils)
n 保存form
設(shè)計(jì)一個(gè)FormUtils
fillForm(HttpServletRequest request , String formFullName , String formName)
Struts標(biāo)簽庫
<bean:define>
從已有的變量或者變量的屬性定義一個(gè)新的變量
Search: from where ~ scope ,who ~ name,which property ~ property
Define:the name of variable newly created ~ id , which is required
Save:to where ~ toScope
<bean:write>
獲得指定bean屬性的值到當(dāng)前的jsp
Three important attributes :
Name: required
Property : name of property
Scope : variable scope
<bean:message>
為響應(yīng)提供國際化字符串消息
資源文件在struts-config.xml中的配置內(nèi)容為:
<message-resources parameter="cn.iego.wudi.ApplicationResources" key="MyKey">
</message-resources>
<bean:message>標(biāo)簽的屬性:
Bundle: 綁定資源文件的key
Key: 返回資源文件中的哪條消息
<logic:iterate>
重復(fù)這個(gè)嵌套在指定集合上的標(biāo)簽體內(nèi)容
Id:是迭代時(shí)的臨時(shí)變量。
兩種查找迭代對象的方式
1、name:所指代的bean必須是一個(gè)集合類型。
2、Name+property:該bean的property指定的屬性,必須是一個(gè)集合類型。
可選屬性
3、Scope:查找的范圍。
4、indexId:元素當(dāng)前位置。
5、Offset:設(shè)置帶點(diǎn)開始點(diǎn),默認(rèn)從0開始。
動(dòng)態(tài)表單
類的全名:
是ActionForm的子類,允許動(dòng)態(tài)設(shè)置屬性的FormBean創(chuàng)建。
感性認(rèn)識:可以不編寫form代碼。
配置動(dòng)態(tài)form需要配置用戶添加屬性。
<form-property>name ~ 屬性名、type ~ 屬性類型、對于primitive type,必須使用包裝類型(wrapper)。
異常處理
當(dāng)Action的execute方法發(fā)生異常的時(shí)候,可以定義一個(gè)ExceptionHandler來處理這個(gè)異常。
因此需要寫一個(gè)
org.apache.struts.action.DynaActionForm
的子類,并且重寫execute方法,返回一個(gè)ActionForward。
在struts-config.xml中配置文件
org.apache.struts.action.ExceptionHandler
<global-exceptions>
<exception
key="some.key"
type="java.io.IOException"
handler="com.yourcorp.ExceptionHandler"/>
</global-exceptions>
Struts中異常處理的流程圖
Plugin:
1、生命周期方法:init destroy
2、用戶提供setter方法,告訴ActionServlet中心控制器把屬性設(shè)置
應(yīng)用:
在struts啟動(dòng)時(shí)把hibernate加載進(jìn)來(讀取hibernate配置文件和打開hibernate的sessionfactory)。
1、struts jar包+hibernate jar包
2、設(shè)計(jì)一個(gè)類:hibernatePlugin實(shí)現(xiàn)plugin接口
a) 屬性:String hibernateConfigFile
b) 讀取hibernate配置文件
c) 打開sessionFactory
3、在struts-config配置文件中添上一堆<plugin>,在plugin中加子標(biāo)簽。
設(shè)計(jì):
可以在中心控制器ActionServlet的init方法對plugin初始化
在destory方法中對plugin銷毀