struts2學習筆記之八(result),struts2學習筆記

來源:互聯網
上載者:User

struts2學習筆記之八(result),struts2學習筆記
strust2的開發步驟

  • 編寫一個發送請求的頁面,表單或超連結
  • 編寫一個action類
    1. 推薦實現action介面或繼承actionsupport
    2. 為所有請求參數,包括需要傳到下一個頁面顯示的資料提供filed
    並提供相應的setter和getter
    3. 要有無參的構造器
    4. 處理請求的方法,該方法不能有形參聲明
    5. 處理請求的傳回值是String
  • 在struts.xml檔案配置action
struts.xml配製方法
                  --------------------                    |                    -------根據邏輯視圖跳轉物理視圖2                    |                                   |                    -------根據邏輯視圖跳轉物理視圖1                    ^                    |  邏輯視圖<--------------------                    |                             |                    |                             | 發送請求----->struts分發器---->建立action執行個體,調用execute
  • action只是負責struts2控制器的部分,所以它不能直接產生瀏覽者的響應
  • action只會負責處理請求,負責產生響應的視圖組件,通常是JSP頁面,而action會為JSP頁面提供顯示的資料
  • 當action處理使用者請求後,控制器根據<result.../>元素決定具體使用哪個資源產生響應
  • 配置邏輯視圖和物理視圖之間的映射關係是通過result子項目來定義的。每個result定義邏輯視圖和物理視圖之間的一次映射
result使用方法
  • <result .../>支援的屬性
    • name:邏輯視圖名。name屬性的預設是success
    • type:指定物理視圖資源的類型。預設值是dispatch,
      與dispatch對應的是redirect—-重新導向
  • 目前的視圖資源類型可能有(不一定是JSP)
    • Velocity
    • FreeMarker
    • JFreeChart
    • 報表(JasperReports)
    • XSLT
  • result的標準格式
                <result name="" type>                    <param></param>                    <param></param>                    ....                </result>       

如果有如下的配置

        <result name="邏輯視圖" type="視圖配型">            <param name="location">物理視圖位置<param>            <param name="parse">true<param>----是否解析OGNL運算式的值        </result>

就可以簡化為

    <result name="邏輯視圖" type="視圖配型">物理視圖位置</result>
  • 屬性
    • name:指定邏輯視圖名。
      預設是success(題外話:不會偷懶的人不是好的程式員,一個人勤勞並不定是優秀的,重要的是勤奮的由價值,偷懶有時候也是勤奮)
    • type:指定所使用的物理檢視類型
      在struts-default.xml中的struts-default抽象包中:
        <result-types>            <result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>            <result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/>            <result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/>            <result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/>            <result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/>            <result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>            <result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/>            <result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/>            <result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/>            <result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult"/>            <result-type name="postback" class="org.apache.struts2.dispatcher.PostbackResult"/>        </result-types>

從以上可知,type的預設值是dispatch。

  • 重要的類型
    • chain :Action處理完請求之後,轉寄到下一個Action繼續處理,形成一個鏈式處理
    • dispatch
    • redirect
      /********
      forward : 依然使用一次請求,地址欄url沒有變化,請求參數和請求屬性都不會丟失
      redirect:重新發送一次新的請求,就像在地址欄中重新輸入一個url並斷行符號的效果一樣,地址欄的url變化,請求參數和請求屬性都會丟失
      dispatch和redirect的區別也是上面的區別
      *********/
    • redirectAction:重新導向到action
    • stream:做檔案下載
    • plainText:顯示頁面源碼
  • 常用type樣本

1、chain

假設在regist的action裡註冊成功之後跳轉到一個瀏覽所有使用者的action

                            <action name="regist" class="registAction">                                <result type="chain">userList</result>                            </action>                            <action name="userList" class="userListAction">                                <result ...> .... </result>                            </action>

2、redirect

直接“重新導向”到新的URL,產生一次新的請求,原有的請求參數請求屬性都會丟失。
與預設的dispatch(“轉寄”)相應

3、 dispatch
4、plainText

直接顯示視圖頁面的原始碼

                            <action name="login" class="loginAction">                                <!--當處理方法返回success,系統跳轉到welcome.jsp-->                                <result type="plainText">                                    <param name="locaiont">/WEB-INF/content/welcome.jsp</param>                                    <!-- 指定物理視圖資源的編碼字元集-->                                    <param name="charSet">utf-8</param>                                </result>                            </action>

一般沒有太大的用處,主要用於顯示原始碼,教學類型的,為了讓學生看到原始碼學習

5、stream —直接產生“二進位”檔案流

  • result的兩種形式

1、局部
將<result .../>作為<action>的子項目,只對action有效
2、全域
將<result .../>用於全域

        <global-results>                <result type="">                </result>        </global-results>

1、當全域result配置與局部result配置衝突時,局部result會覆蓋全域result
盡量少用全域result,只有在多個action都具有某個通用性質的result,才考慮全域result
2、不同的result類型,可使用的參數不同
/*********
struts的設定檔,包括:
struts-default.xml: 核心包使用該設定檔
struts-plugin.xml: 外掛程式包使用該設定檔
struts.xml : struts2應用使用該設定檔
struts-abc.xml : 其他的設定檔,必須被include載入
**********/
如dispatch的參數有location和parse
需要使用的時候,查閱文檔,

  • 動態結果

使用{1},{2}….

例:

        通用的action        <action name="*">            <result>                /WEB-INF/content/{1}.jsp            </result>        </action>        {1}匹配*所匹配的第一個匹配值        <action name="*Action">            <result>                /WEB-INF/content/{1}_{2}.jsp            </result>        </action>

Action屬性值決定視圖資源
不僅可以使用{1}…,還可以使用${屬性}

            <action name="myPro" class="">                <!--在定義物理視圖資源時,可以直接使用OGNL運算式,所以這個物理視圖可以動態改變。通過OGNL來計算${res}運算式的值                -->                <result>                    /WEB-INF/content/{res}.jsp                </result>
  • 動態方法引動過程(DMI Dynamic Method Invocation)

    用法不是特別大,大致瞭解
    功能:可以在提交請求的時候,直接提交給action的指定方法。

    • 未使用DMI:
      <form action="myPro" method="post"></form>

    上述代碼只能看出提交給哪個action,而無法看到提交給哪個方法,
    具體調用的需要查看struts.xml

    • 使用DMI:
      可以直接在action後增加”!method”的形式,來DMI
      <form action="myPro!login" method="post"></form>
      所謂的DMI其實就是在頁面上指定請求地址為:actionName!methodName

    缺點:
    1、安全性不高
    2、沒有必要,完全可以在action元素中指定method來代替動態方法引動過程

第一次用markdown的文法寫,總體感覺還是可以,就是有序列表不是很好,或許是自己沒用好,以後多多體會

聯繫我們

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