學習筆記

          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 閱讀(175) 評論(0)  編輯  收藏


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


          網站導航:
           
          主站蜘蛛池模板: 华宁县| 清丰县| 厦门市| 蓝山县| 兖州市| 富平县| 互助| 泉州市| 长宁区| 扎赉特旗| 大城县| 普兰店市| 曲阳县| 中卫市| 获嘉县| 长汀县| 观塘区| 余干县| 朝阳区| 偃师市| 南开区| 罗田县| 阜平县| 靖安县| 甘洛县| 阿克苏市| 庆元县| 苍南县| 南岸区| 潞西市| 改则县| 山西省| 静海县| 繁峙县| 辉南县| 清徐县| 崇信县| 富源县| 醴陵市| 金寨县| 阿合奇县|