Struts 學習筆記之Action

來源:互聯網
上載者:User
Struts 學習筆記之Action  下面是Struts中的一些常用Action如DispatchAction/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,則頁面提交時我們必須指定提交時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 onclick="submitForm('create');">CREATE</html:button> <html:button onclick="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>

 

  著作權:(xiaodaoxiaodao)藍小刀    xiaodaoxiaodao@gmail.com

 

聯繫我們

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