【web開發學習筆記】Structs2 Action學習筆記(一)

來源:互聯網
上載者:User

1、org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter準備和執行
2、

  <filter-mapping>      <filter-name>struts2</filter-name>      <url-pattern>/*</url-pattern>  </filter-mapping>
    url-pattern約定熟成唯寫/*,沒必要寫*.action
3、
<package name="default" namespace="/" extends="struts-default">     <action name= "hello">         <result>         /Hello.jsp         </result>     </action>    <span style="white-space:pre"></span></package>
   namespace="/"和訪問的路徑意義對應; 
    /hello.action
    訪問了action,給了我們result的結果;
    預設的action可以省略;
4、關聯structs原始碼和java docs
    jar檔案右鍵代->properies->Java Source Attachment
    設定源碼
        D:/Program Files/struts-2.1.6/src/core/src/main/java
    javadoc 文檔
        file:/D:/Program Files/struts-2.1.6/docs/struts2-core/apidocs/
    設定xml提示:
        a)window – preferences – 搜尋 catalog – add 

       b)選擇key type為URI

        c)key: http://struts.apache.org/dtds/struts-2.0.dtd

        d)location: 對應的dtd檔案,位於struts-core包中,解壓開,指定相應位置,如D:/Program Files/struts-2.1.6 \lib\struts2-core-2.1.6\struts-2.0.dtd
5、structs運行機制
    用戶端 -> url -> Http請求 -> Tomacat -> 尋找對應的Webapplication -> web.xml -> filter doFilter方法->
  <filter>  <filter-name>struts2</filter-name>  <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  </filter>  <filter-mapping>  <filter-name>struts2</filter-name>  <url-pattern>/*</url-pattern>  </filter-mapping>
    ->參考struct.xml -> 尋找對應的namespace -> 尋找對應的action -> 尋找對應的result -> 反饋result -> 請求Forwad給目標檔案 
  <package name="default" namespace="/" extends="struts-default"><action name="index"><result>/index.jsp</result></action> </package>
6、namespace決定了action的訪問路徑,預設為"",可以接收所有路徑的action
    namespace可以寫為/,或者/xxx,或者/xxx/yyy,
    對應的action訪問路徑為/index.action,/xxx/index.action,或者/xxx/yyy/index.action.
    namespace最好也用模組來進行命名 
   <constant name="struts.devMode" value="true" />    <package name="front" extends="struts-default" namespace="/front">        <action name="index">            <result>/Namespace.jsp</result>        </action>    </package>         <package name="main" extends="struts-default" namespace="">        <action name="index">            <result>/Namespace.jsp</result>        </action>    </package>
    struct.xml分析
        package作用:避免action的重名和衝突問題;
        namespace="/front",必須以斜杠開頭,namespace最好也用模組來進行命名;
        不寫namespace,等效於namespace = "",意味著只要找到action,全都交給
        namespace處理;
                流程:先找對應路徑下的action進行匹配,如果沒有的話找namespace為空白的action,
                如果還沒有找到action則會報錯;
7、 具體視圖的返回可以由使用者自己定義的Action來決定;
    具體的手段是根據返回的字串找到對應的配置項,來決定視圖的內容;
<constant name="struts.devMode" value="true" /><package name="front" extends="struts-default" namespace="/"><action name="index" class="com.struts2.front.action.Index"><result name="success">/ActionIntroduction.jsp</result></action> </package>
    具體Action的實現可以是一個普通的java類,裡面有public String execute方法即可或者實現
    Action介面;
    package com.bjsxt.struts2.front.action;    import com.opensymphony.xwork2.Action;    public class IndexAction1 implements Action {@Overridepublic String execute() {return "success";}    }
    配置分析:
            <class="com.bjsxt.struts2.front.action.Index">
            struct.xml -> 找到對應的class -> 執行個體化對象 -> 執行對應的execute()方法
    執行過程:
        讀到xml -> action是class -> 找到class對象(每次訪問必須new一個對象) -> 
        當不配置class的時候,預設的class是ActionSupport。

        ActionSupport源碼

   public String execute() throws Exception {        return SUCCESS;}


    最常用的是從ActionSupport繼承,好處在於可以直接使用Struts2封裝好的方法

    package com.struts2.front.action;    import com.opensymphony.xwork2.ActionSupport;    public class IndexAction2 extends ActionSupport {@Overridepublic String execute() {return "success";}    }

        原因:ActionSupport內部已經為我們封裝了許多可以直接調用的方法,在子類中可以直接使用。

以上內容是基於馬士兵老師的教程整理而成的。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.