版權所有:(xiaodaoxiaodao)藍小刀 ?? xiaodaoxiaodao@gmail.com

http://www.tkk7.com/xiaodaoxiaodao/archive/2007/06/10/123141.html ????? ??

轉載請注明來源/作者

?

Struts 學習筆記之Action

?

下面是Struts中的一些常用ActionDispatchAction/LookupDispatchAction/MappingDispatchAction/ForwardAction/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 onclick="set('create');">CREATE</html:submit>

<html:submit onclick="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,則頁面提交時我們必須指定提交時actionmethod參數來確定去我們想要調用哪個Action方法。

<script>

??? function submitForm(operation) {

?????? ?with (document.forms[0]) {

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

??????????? submit();

??????? }

??? }

</script>

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

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

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

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

</html:form>

?

2 LookupDispatchAction extends DispatchAction

LookupDispatchAction 繼承DispatchAction, 在上面的 中對于同一個頁面上的多個submit按鈕,不需要那么多復雜的js函數來指定提交時actionmethod參數,即上面的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()方法, ResKeyMethodName對應起來, 如下:

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,則頁面提交時我們必須指定提交時actionmethod參數來確定去我們想要調用哪個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.xmlparameter的值。

?

4 ForwardAction extends BaseAction

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

<action path="/listUser"

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

?????? ?scope="request"

??????? parameter="/pages/listUser.jsp">

</action>

parameter 屬性用于指定forward到哪個頁面,pathtypeparameter三個屬性為必須,其他可省略。

?

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>

?

?

版權所有:(xiaodaoxiaodao)藍小刀 ?? xiaodaoxiaodao@gmail.com

?