學習筆記

          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)  編輯  收藏


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


          網站導航:
           
          主站蜘蛛池模板: 金华市| 上虞市| 博罗县| 屯昌县| 墨玉县| 定南县| 治多县| 绥阳县| 永年县| 岱山县| 犍为县| 特克斯县| 佛学| 新竹县| 泗洪县| 岚皋县| 民勤县| 壤塘县| 衡南县| 揭东县| 襄樊市| 宁城县| 英吉沙县| 雅安市| 甘南县| 科尔| 平乐县| 玉山县| 刚察县| 建始县| 桃园市| 马鞍山市| 张家川| 六盘水市| 辽阳市| 阳朔县| 黔江区| 彝良县| 康马县| 高碑店市| 太白县|