JavaEE筆記(六)

來源:互聯網
上載者:User

標籤:import   oca   pre   struts   result   test   static   str   筆記   

實現Action的幾種方法
1. implements Action
2. extends ActionSupport
3. 也可以不繼承任何父類不實現任何借口

#當一個類有多個方法

package com.struts.test;import com.opensymphony.xwork2.ActionSupport;public class Test extends ActionSupport {    private static final long serialVersionUID = 1L;    @Override    public String execute() throws Exception {        System.out.println("execute");        return SUCCESS;    }        public String add() throws Exception {        System.out.print("add");        return "add";    }        public String update() throws Exception {        System.out.print("update");        return "update";    }        public String delete() throws Exception {        System.out.print("delete");        return "delete";    }}

如上方法,裡面有add()、update()、delete();
這麼多方法該如何配置?
1. 配置多個action

<package name="struts_two" extends="struts-default">        <!-- 執行Test類中的update方法 -->    <action name="test_one_struts" class="com.struts.test.Test" method="update">        <!-- 返回跳轉到Test_update.jsp頁面 -->        <result name="update">/Test_update.jsp</result>    </action>        <!-- 執行Test類中的delete方法 -->    <action name="test_one_struts" class="com.struts.test.Test" method="delete">        <!-- 返回跳轉到Test_delete.jsp頁面 -->        <result name="delete">/Test_delete.jsp</result>    </action>        <!-- 執行Test類中的add方法 -->    <action name="test_one_struts" class="com.struts.test.Test" method="add">        <!-- 返回跳轉到Test_add.jsp頁面 -->        <result name="add">/Test_add.jsp</result>    </action></package>

2.動態配置(不推薦)

<!--#2.5版本需要開啟動態配置<constant value="true" name="struts.enable.DynamicMethodInvocation"/>--><package>    <action name="test_one_struts" class="com.struts.test.Test">        <result name="update">/Test_update.jsp</result>        <result name="delete">/Test_delete.jsp</result>        <result name="add">/Test_add.jsp</result>    </action></package>

3.萬用字元配置(推薦)

<package>    <!--    * 萬用字元    根據你輸入的路徑來判斷你是執行什麼方法    比如:你輸入 Test_delete,那麼就會執行Test_類的delete方法,並且跳轉到對應的頁面    -->    <action name="Test_*" class="com.struts.test.Test" method="{1}">        <result name="{1}">/Test_{1}.jsp</result>    </action></package>

JavaEE筆記(六)

聯繫我們

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