學習筆記

          Simple is beautiful.

          導航

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

          統(tǒng)計

          公告

          ...

          常用鏈接

          留言簿(1)

          隨筆分類(2)

          隨筆檔案(56)

          Weblog

          搜索

          最新評論

          評論排行榜

          使用DispatchAction類,為系統(tǒng)減肥!

          使用DispatchAction類,為系統(tǒng)減肥!

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

          例如我們可以繼承 DispatchAction 來定義一個 AccountAction ,在當中集中管理一些與賬號相關(guān)的操作,如下:
          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
          相關(guān)的操作

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

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

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


          <ACTION

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

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

          %>

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


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


          網(wǎng)站導航:
           
          主站蜘蛛池模板: 大连市| 江北区| 江达县| 英山县| 越西县| 绥宁县| 青岛市| 海盐县| 化德县| 永登县| 盐津县| 万年县| 庄浪县| 岑溪市| 鸡泽县| 南江县| 赤城县| 武城县| 仁布县| 涿鹿县| 紫金县| 牟定县| 锡林郭勒盟| 剑川县| 法库县| 南京市| 保康县| 伊金霍洛旗| 亳州市| 高州市| 商丘市| 德保县| 石门县| 涡阳县| 莱西市| 稷山县| 遵义市| 濉溪县| 西和县| 佛冈县| 怀柔区|