Struts1 - 學習筆記 - Action

下面是Struts中的一些常用Action的總結,如:

  1. DispatchAction
  2. LookupDispatchAction
  3. MappingDispatchAction
  4. ForwardAction
  5. IncludeAction

 

1 .DispatchAction extends BaseAction

一般的Action如<action path="/createUser" type="examples.UserAction">,在這裏UserAction只需要繼承父類(extends Action類),然後重寫父類的execute方法,在execute中實現具體的控制轉向。

對於同一個formbean上進行的新增、修改、刪除等,我們需要分發不同的Action,這裏有兩種做法。

一種是通過在execute方法中if判斷進行不同的轉向:

UserAction 類的execute方法中

String method = request.getParameter("method");

if (method.equals("create")) {

     ……

    return mapping.findForward("createUser");

}

if (method.equals("save")) {

    ……

    return mapping.findForward("saveUser");

}

 

struts-config.xml 中:

<action path="/createUser" type="examples.UserAction"

        name="userForm"

        scope="request">

    <forward name="createUser" path="/pages/listUser.jsp"/>

</action>

<action path="/saveUser" type="examples.UserAction"

        name="userForm"

        scope="request">

    <forward name="saveUser" path="/pages/saveUser.jsp"/>

</action>

 

可以在頁面中定義一個隱藏變量來指明相應的操作

// 這裏最好不要使用<html:hidden property="method"/>

// 因爲這種寫法需要在formbean中定義相應的property,我們可以採用普通隱藏域

<input type="hidden" name="method"/>

然後定義一個javascript函數,我們可以在通過點擊提交按鈕的時候,在函數中修改它的值。

<script>

    function set(operation) {

        with (document.forms[0]) {

            method.value = operation;

        }

    }

</script>

點擊提交按鈕時,通過set方法設置提交的method屬性值:

<html:submit οnclick="set('create');">CREATE</html:submit>

<html:submit οnclick="set('save');">SAVE</html:submit>

 

 

第二就是使UserAction繼承DispatchAction,不需要重寫execute方法:

public ActionForward create(ActionMapping mapping,

                           ActionForm form,

                           HttpServletRequest request,

                           HttpServletResponse response)

        throws Exception {

    // 進行一些create的邏輯

    // ……

    return mapping.findForward("createUser");

}

public ActionForward save(ActionMapping mapping,

                           ActionForm form,

                           HttpServletRequest request,

                           HttpServletResponse response)

        throws Exception {

    // 進行一些save的邏輯

    // ……

    return mapping.findForward("saveUser");

}

 

DispatchAction 在配置上和一般Action稍有不同,就是要在Action配置中多一個parametr屬性,這個屬性可以指定執行DispatchAction中對應的方法。

struts-config.xml 中:

<action path="/processUser" type="examples.UserAction"

        name="userForm"

        scope="request"

        parameter="method">

    <forward name="createUser" path="/pages/listUser.jsp"/>

    <forward name="saveUser" path="/pages/saveUser.jsp"/>

</action>

 

我們在這裏指定了parameter的值爲method,則頁面提交時我們必須指定提交時action的method參數來確定去我們想要調用哪個Action方法。

<script>

    function submitForm(operation) {

        with (document.forms[0]) {

            action = action + '?method = '+ operation;

            submit();

        }

    }

</script>

點擊提交按鈕時,通過submitForm方法設置提交時action的method參數:

<html:form action="/processUser" method="get">

<html:button οnclick="submitForm('create');">CREATE</html:button>

<html:button οnclick="submitForm('save');">SAVE</html:button>

</html:form>

 

2 . LookupDispatchAction extends DispatchAction

LookupDispatchAction 繼承DispatchAction, 在上面的 中對於同一個頁面上的多個submit按鈕,不需要那麼多複雜的js函數來指定提交時action的method參數,即上面的submitForm(operation)方法可以省去,LookupDispatchAction其用法爲:

用MessageResource將按鈕的文本和ResKey相關聯,例如button.save=保存;中用LookupDispatchAction代替就是:

<html:form action="/processUser" method="get">

<html:submit property="method ">

    <bean:message key="button.create "/>

</html:submit>

<html:submit property="method ">

    <bean:message key="button.save "/>

</html:submit>

</html:form>

 

在Action配置中多一個parametr屬性,屬性值與submit按鈕的property屬性值相同,這個屬性可以指定執行LookupDispatchAction中對應的方法。

struts-config.xml 中:

<action path="/processUser " type="examples.UserAction"

        name="userForm"

        scope="request"

        parameter="method ">

    <forward name="createUser" path="/pages/listUser.jsp"/>

    <forward name="saveUser" path="/pages/saveUser.jsp"/>

</action>

 

使UserAction繼承LookupDispatchAction,重寫getKeyMethodMap()方法, 將ResKey和MethodName對應起來, 如下:

protected Map getKeyMethodMap() {

    Map map = new HashMap();

    map.put("button.create", "create");

    map.put("button.save", "save");

    return map;

}

 

注: DispatchAction 類使用請求參數的值確定調用哪種方法,而LookupDispatchAction類利用請求參數值,反向查詢資源綁定,並將它與類中的一種方法匹配,實際上這兩種方法有異曲同工之處。

 

3 . MappingDispatchAction extends DispatchAction

DispatchAction 指定了parameter的值爲method,則頁面提交時我們必須指定提交時action的method參數來確定去我們想要調用哪個Action方法,而MappingDispatchAction直接通過struts-config.xml將多個action-mapping映射到同一個Action類的不同方法:

<action path="/createUser" type="examples.UserAction"

        name="userForm"

        scope="request"

        parameter="create">

    <forward name="createUser" path="/pages/listUser.jsp"/>

</action>

<action path="/saveUser" type="examples.UserAction"

        name="userForm"

        scope="request"

        parameter="save">

    <forward name="saveUser" path="/pages/saveUser.jsp"/>

</action>

 

然後UserAction繼承MappingDispatchAction即可:

public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception

public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception

 

注: 查看MappingDispatchAction的源碼:

protected String getMethodName(ActionMapping mapping, ActionForm form,

    HttpServletRequest request, HttpServletResponse response,

    String parameter) throws Exception {

    // Return the unresolved mapping parameter.

    return parameter;

}

可以看到它調用的方法是直接返回struts-config.xml中parameter的值。

 

4 . ForwardAction extends BaseAction

相當於<jsp:forward>功能,不需要配置formbean和action,可以直接進行跳轉,只需要在struts-config.xml中配置:

<action path="/listUser"

        type="org.apache.struts.actions.ForwardAction"

        scope="request"

        parameter="/pages/listUser.jsp">

</action>

parameter 屬性用於指定forward到哪個頁面,path、type、parameter三個屬性爲必須,其他可省略。

 

5 . IncludeAction extends BaseAction

相當於<jsp:include>功能,需要在struts-config.xml中配置:

<action path="/listUser" type="org.apache.struts.actions.IncludeAction"

        name="userForm"

        scope="request"

        parameter="/pages/includepage.jsp">

</action>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章