Struts學習筆記:程式的組件設計與分析

來源:互聯網
上載者:User
文章目錄
  • ActionForm據官方文檔介紹應該歸類到控制器
  • 關於Action要注意的問題:
  • ActionMapping——>對struts-config中<action-mapping>的封裝
  • ActionForward(導航器)
  • ActionForm的工作原理:處理ActionForm的一般步驟

根據上篇部落格的工作原理,來看看一個具體的簡單的小例子的分析,使用struts增加學生,如:

 

很簡單的一個例子,源碼下載:http://download.csdn.net/detail/duancanmeng/4517024,代碼一看就懂,重要的是上面的思路要理清楚。

 

ActionForm據官方文檔介紹應該歸類到控制器

----------------------------------------------------------------------------------------------------華麗的分割線----------------------------------------------------------------------------------------------------------------

關於Action要注意的問題:

1,Action什麼時候初始化?

    是在使用者發出該Action請求時候,而不是在讀取配置時初始化。

 

2,初始化幾次?

    總共初始化一次,在使用者第一次請求時候,不論struts-config.xml中有多少action的type都指向該action類,都只會初始化一次。

 

3,如果有兩個Action的配置中的type都指向同一個action,action只初始化一次,也就是說多個action請求進入了同一個action類,這樣容易造成安全執行緒問題,如何解決?

    *    如果這個資訊只是用於某一個請求的,這個時候我們就不能用執行個體變數或者類變數;相反,如果我所有的請求剛好又需要共用某個資訊,就可以用執行個體變數或靜態變數來儲存

    *    存取其他資源(javabean,session等)必須同步,如果這些資源需要保護的話。

    *    struts與struts2的區別,action安全和不安全是很大的一點

    *    來看一個簡單例子:統計一個action訪問次數,安全性問題的產生和解決圖示:

 

----------------------------------------------------------------------------------------------------華麗的分割線----------------------------------------------------------------------------------------------------------------

ActionMapping——>對struts-config中<action-mapping>的封裝

----------------------------------------------------------------------------------------------------華麗的分割線----------------------------------------------------------------------------------------------------------------

ActionForward(導航器)

redirect:

    False——>容器內跳轉.RequestDispatcher:forward

    True——>容器外跳轉.HttpResponse:sendRedirect   .跳轉時需要帶上絕對路徑,例如 path=http://www.baidu.com

----------------------------------------------------------------------------------------------------華麗的分割線----------------------------------------------------------------------------------------------------------------

ActionForm的工作原理:處理ActionForm的一般步驟

1、檢查Action映射,確定Action中已經配置了對ActionForm的映射

2,、根據name屬性,尋找formbean的配置資訊

3、檢查Action的formbean的使用範圍,確定在此範圍下(request,session),是否已經由此formbean的執行個體

4、假如當前範圍下,已經存在了此formbean的執行個體,而且對當前請求來說,是同一種類型的話,那麼就重用

5、否則,就重新構建一個formbean的執行個體(調用構造方法),並且儲存在一定作用範圍

6、formbean的reset方法被調用

7、調用對應的setter方法,對狀態屬性賦值

8、如果validate的屬性設定為true,那麼就調用formbean的validate方法

9、如果validate方法沒有返回任何錯誤,控制器將ActionForm作為參數,傳給Action執行個體的execute方法並執行

注意:直接從ActionForm類繼承的reset和validate方法並不能實現什麼處理功能,所以有必要自己覆蓋。

 

由圖也可以看出,驗證在賦值之後,execute方法之前執行。

分析上面的流程圖中,如何來證明form存了進去?在scope.setAttribute(name,form)處

1、從過程看:實現監聽,進行監視

    HttpSessionAttributeListener

    ServletRequestAttributeListener

    上面兩個介面分別可以捕捉到scope為request和session不同情況的屬性動態

public class AttributeListener implements HttpSessionAttributeListener,ServletRequestAttributeListener {@Overridepublic void attributeAdded(ServletRequestAttributeEvent arg0) {System.out.println("add request attribute");if(arg0.getValue() instanceof ActionForm){System.out.println("this is request add mothed");System.out.println(arg0.getName()+","+arg0.getValue());}}@Overridepublic void attributeRemoved(ServletRequestAttributeEvent arg0) {System.out.println("Removed request attribute");if(arg0.getValue() instanceof ActionForm){System.out.println("this is request Removed mothed");System.out.println(arg0.getName()+","+arg0.getValue());}}@Overridepublic void attributeReplaced(ServletRequestAttributeEvent arg0) {System.out.println("Replaced request attribute");if (arg0.getValue() instanceof ActionForm) {System.out.println("this is request Replaced mothed");System.out.println(arg0.getName() + "," + arg0.getValue());}}@Overridepublic void attributeAdded(HttpSessionBindingEvent arg0) {System.out.println("Add session attribute");if (arg0.getValue() instanceof ActionForm) {System.out.println("this is session Add mothed");System.out.println(arg0.getName() + "," + arg0.getValue());}}@Overridepublic void attributeRemoved(HttpSessionBindingEvent arg0) {System.out.println("Removed session attribute");if (arg0.getValue() instanceof ActionForm) {System.out.println("this is session Removed mothed");System.out.println(arg0.getName() + "," + arg0.getValue());}}@Overridepublic void attributeReplaced(HttpSessionBindingEvent arg0) {System.out.println("Replaced session attribute");if (arg0.getValue() instanceof ActionForm) {System.out.println("this is session Replaced mothed");System.out.println(arg0.getName() + "," + arg0.getValue());}}}

我們這裡只關心ActionForm

可以看到action的設定檔中:

<action path="/add" type="com.pccw.action.AddStudentAction" name="studentForm"><forward name="addsuccess" path="/addsuccess.jsp"></forward><forward name="addfail" path="/addfail.jsp"></forward></action>

這裡的scope沒有設定,預設值為:session,所以我們可以看到當請求過來的時候列印的資訊:

這是在session的add方法中監控到的。

 

2、從結果去看

    在execute中直接從request來得到actionForm,然後通過與execute中的參數form進行比較,會發現,這兩個東西是同一個東西。

public class AddStudentAction extends Action{@Overridepublic ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws Exception {StudentForm studentForm = (StudentForm)form;StudentDao dao = new StudentDaoImpl();boolean isSave = dao.addStudent(studentForm);String returnKeyValue = "addfail";if(isSave) returnKeyValue = "addsuccess";StudentForm studentForm2 = null;String scope = mapping.getScope();if(scope.equals("request")){studentForm2 = (StudentForm) request.getAttribute("studentForm");}else{studentForm2 = (StudentForm) request.getSession().getAttribute("studentForm");}System.out.println(studentForm == studentForm2);  //一個是參數,一個是從request中擷取的return mapping.findForward(returnKeyValue);}}

列印結果我們可以看到:

列印結果為true,說明是同一個東西,不緊緊是內容相同,而且地址相同。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.