學習筆記

          Simple is beautiful.

          導航

          <2007年2月>
          28293031123
          45678910
          11121314151617
          18192021222324
          25262728123
          45678910

          統計

          公告

          ...

          常用鏈接

          留言簿(1)

          隨筆分類(2)

          隨筆檔案(56)

          Weblog

          搜索

          最新評論

          評論排行榜

          使用DispatchAction類,為系統減肥!

          使用DispatchAction類,為系統減肥!

          Struts 中你要盡可能的不用 Action 類,因為他們讓你的項目變得臃腫,你可以使用 org.apache.struts.actions.DispatchAction 類來完成業務邏輯所需要的相關操作集中到一個 Action 類中,在繼承 DispatchAction 后,你不再是重新定義 execute() 方法,而是編寫你自己的業務方法, execute() 方法在 DispatchAction 抽象類定義。

          例如我們可以繼承 DispatchAction 來定義一個 AccountAction ,在當中集中管理一些與賬號相關的操作,如下:
          package com.fasttalk;

          import javax.servlet.http.*;
          import org.apache.struts.action.*;
          import org.apache.struts.actions.*;
          ?????????????????????????????????????????????????????????
          public class AccountAction extends DispatchAction {
          ??? public ActionForward login(ActionMapping mapping,
          ????????????????????????????? ActionForm form,
          ????????????????????????????? HttpServletRequest request,
          ????????????????????????????? HttpServletResponse response)
          ??? throws Exception {
          ??????? // login
          相關的操作

          ??????? ......
          ??? }
          ????public ActionForward logout(ActionMapping mapping,
          ??????????????????????????????? ActionForm form,
          ??????????????????????????????? HttpServletRequest request,
          ??????????????????????????????? HttpServletResponse response)
          ??? throws Exception {
          ??????? // logout相關的操作

          ??????? ......
          ??? }
          ????public ActionForward method1(ActionMapping mapping,
          ??????????????????????????????? ActionForm form,
          ???? ???????????????????????????HttpServletRequest request,
          ??????????????????????????????? HttpServletResponse response)
          ??? throws Exception {
          ??????? // method1相關的操作

          ??????? ......
          ???? }
          ???......
          }
          我們不再重新定義execute()方法,而是定義我們自己的login()logout()等方法,
          這些方法接收與execute()相同的參數,并且也傳回ActionForward對象。
          使用DispatchAction時,我們要在struts-config.xml定義:


          <ACTION

          ??????????? path="/account"
          ??????????? type="com.fasttalk.AccountAction"
          ??????????? parameter="method"
          ??????????? name="userForm">
          使用方法:
          通過參數method= method1 來選擇函數

          <%
          ??????String dest = " account.do?method= method1";
          ???response.sendRedirect(dest);

          %>

          posted on 2007-02-18 18:24 Ecko 閱讀(180) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 桑日县| 阿克陶县| 鄯善县| 南昌县| 德州市| 湟源县| 双桥区| 泗阳县| 平顶山市| 永安市| 周至县| 永和县| 敖汉旗| 苏尼特右旗| 大新县| 乐陵市| 潼南县| 鲁山县| 尤溪县| 巨鹿县| 兴安盟| 台南市| 宜州市| 习水县| 镇康县| 朝阳县| 酒泉市| 遂溪县| 高青县| 屯门区| 梨树县| 麻江县| 文水县| 枣庄市| 天峨县| 时尚| 灵璧县| 南澳县| 东乡| 安泽县| 土默特左旗|