Struts2知識點總結:
1、 struts2的體系結構:充當中央控制器的核心過濾器FilterDispather、充當線程清潔工的ActionContextCleanup、決定是否調用自定義業務控制器Action的action映射器、根據配置文件創建Action執行環境的Action代理、action執行環境包括struts2標籤庫在內的struts2視圖組建。
ActionContextCleanup過濾器是struts2的一個常用輔助類,主要用於處理清除當前線程的ActionContext和dispather,防止內存泄露。在web.xml中配置
<filter>
<filter-name>struts2-cleanup</filter-name>
<filter-class>org.apache.struts2.dispatcher.ActionContextCleanup</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2-cleanup</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
核心控制器FilterDispatcher是struts2框架的基礎,包涵了框架內部的控制流程和處理機制。
FilterDispatcher在web.xml中的配置代碼如下
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatecher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
2、 struts2常用類介紹
Action接口:通過實現接口Action可以快速的開發業務控制器Action類,具體的業務邏輯在execute()方法中編寫,調用ActionContext.getContext()可取得ServletContext的引用,進而取得HttpServletRequest及httpsession對象的訪問。爲規範Action處理結果的result的命名,Action接口定義了5個常用的字符串常量:success、none、error、input、login
ActionSupport類實現類Action接口和Validate接口,因此通過繼承該類也可以簡化業務邏輯控制器Action的開發,具體的業務邏輯放在execute中執行,數據驗證則放在validate方法中。
ActionContext類:是struts2訪問ServletApi(HpptServletRequest、httpsession、servletcontext)提供的工具類,同時也是struts2中的默認Action類。
方法:getObject(key)得到HttpServletRequest對象中的指定屬性
getContext()返回綁定到當前線程特定的ActionContext
getParameters() 得到所請求的參數;
getSession()得到一個HttpSession的模擬對象
getApplication()得到一個ServletContext的模擬對象
ServletActionContext類:struts2爲直接訪問ServletApi提供的工具類。
getPageContext()得到當前web應用中的pagecontext對象;
getRequest()得到當前web應用中的httpservletrequest對象
getresponse()得到當前web應用中HttpservletResponse對象
getServletContext()得到當前web應用中的ServletContext對象。