posts - 188,comments - 176,trackbacks - 0

          對于struts框架,我們都知道我們寫的*Action類是繼承struts的Action(org.apache.struts.action.Action)類,并重寫其定義的execute方法,進而來實現我們自己的業務邏輯。

          但考慮到到一些需求,我們可以在struts的Action和我們自己寫的*Action類之間加一層Action類來實現過濾功能,我們將其定義為BaseAction,整個繼承關系:*Action--extends---->BaseAction---extends--->Action。

          舉例如下:

          在ListAction類和Struts框架的Action類之間加一層BaseAction類。

          ListAction:

          //extends BaseAction
          public class ListAction extends BaseAction {

           public ActionForward doExecute(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) throws Exception {
            
            Connection conn = null;
            conn = this.getDataSource(request).getConnection();
            BookDAO bookDAO  = DAOFactory.getBookDAO(conn);
            List list = bookDAO.findAll();
            request.setAttribute("books", list);
            return mapping.findForward(Constants.FORWARD_LIST); 
           }
           
           //實現BaseAction中的needLogin方法,判斷*Action是否需要登陸驗證
           public boolean needLogin() {  
            return true;
           }
          }

          BaseAction:

          public abstract class BaseAction extends Action {

            //override the method execute of Action  
           public final ActionForward execute(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception {

            if(needLogin()){
               //取出LoginAction中放入session中的用戶名"aaa"
               Object o = request.getSession().getAttribute("aaa");
             if(o == null){
               //登陸驗證失敗,返回login.jsp
               return actionMapping.findForward("login");
             }
            } 
            //返回調用ListAction中的doExcute()方法,執行業務邏輯
            return doExecute(actionMapping, actionForm, request, response);
           }

           //abstract method
           public abstract ActionForward doExecute(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest  request, HttpServletResponse response)  throws Exception;
           
           //abstract method
           public abstract boolean needLogin();

          }

          在BaseAction中重寫struts中Action的excute()方法,在ListAction中定義doexcute()方法并extends BaseAction類,登陸系統是根據login.do進入LoginAction,調用重寫struts中的excute()方法,此時LoginAction沒有,就到父類BaseAction去調用excute()方法,執行其業務邏輯后,如過用戶是已經登陸過就返回doExcute()方法,回到ListAction調用doExcute()的業務邏輯,如果是非登陸用戶則直接轉向Login.jsp。這里將java中多態,抽象類以及回調的思想體現得淋漓盡致。


          posted on 2007-05-25 10:16 cheng 閱讀(4479) 評論(11)  編輯  收藏 所屬分類: Struts

          FeedBack:
          # re: BaseAction的java多態思考
          2007-05-25 10:59 | framework
          這種方式很好的,但實際應用時要比這個復雜, 要進行方法分派  回復  更多評論
            
          # re: BaseAction的java多態思考
          2007-05-25 11:03 | cheng
          恩,這是一個基本的實現,個人感覺關鍵是思想意識的形成。
          當然具體問題肯定就要具體去分析了~~~
          因為還沒有接觸到大的項目,所以,呵呵。目前還沒有體會到你那種意境了~~~
          只有等以后做項目當中去體會和提升了~!  回復  更多評論
            
          # re: BaseAction的java多態思考
          2007-05-25 11:29 | dennis
          把權限的判斷分散到各個action中并不是個好主意,用filter可以搞定的事情沒必要這么復雜  回復  更多評論
            
          # re: BaseAction的java多態思考[未登錄]
          2007-05-25 11:46 | Aqing
          用過濾器實現比較簡單:

          import javax.servlet.*;
          import javax.servlet.http.HttpServletRequest;
          import javax.servlet.http.HttpServletResponse;
          import javax.servlet.http.HttpSession;
          import java.util.List;
          import java.util.ArrayList;
          import java.util.StringTokenizer;
          import java.io.IOException;

          /**
          * 用于檢測用戶是否登陸的過濾器,如果未登錄,則重定向到指的登錄頁面 配置參數 redirectURL 如果用戶未登錄,則重定向到指定的頁面,URL不包括
          * ContextPath notCheckURLList不做檢查的URL列表,以分號分開,并且 URL 中不包括 ContextPath
          * checkSessionKey 需檢查的在 Session 中保存的關鍵字
          */

          public class CheckLoginFilter implements Filter {
          protected FilterConfig filterConfig = null;

          private String redirectURL = null;

          private String sessionKey = null;

          private List notCheckURLList = new ArrayList();

          public void init(FilterConfig filterConfig) throws ServletException {
          this.filterConfig = filterConfig;
          redirectURL = filterConfig.getInitParameter("redirectURL");
          sessionKey = filterConfig.getInitParameter("checkSessionKey");

          String notCheckURLListStr = filterConfig
          .getInitParameter("notCheckURLList");

          if (notCheckURLListStr != null) {
          StringTokenizer st = new StringTokenizer(notCheckURLListStr, ";");
          notCheckURLList.clear();
          while (st.hasMoreTokens()) {
          notCheckURLList.add(st.nextToken());
          }
          }
          }

          public void doFilter(ServletRequest servletRequest,
          ServletResponse servletResponse, FilterChain filterChain)
          throws IOException, ServletException {
          HttpServletRequest request = (HttpServletRequest) servletRequest;
          HttpServletResponse response = (HttpServletResponse) servletResponse;
          HttpSession session = request.getSession();

          if (sessionKey == null) {
          filterChain.doFilter(request, response);
          return;
          }
          if ((!checkRequestURIIntNotFilterList(request))
          && session.getAttribute(sessionKey) == null) {
          response.sendRedirect(request.getContextPath() + redirectURL);
          return;
          }
          filterChain.doFilter(servletRequest, servletResponse);
          }

          private boolean checkRequestURIIntNotFilterList(HttpServletRequest request) {
          String uri = request.getServletPath()
          + (request.getPathInfo() == null ? "" : request.getPathInfo());
          return notCheckURLList.contains(uri);
          }

          public void destroy() {
          notCheckURLList.clear();
          }
          }  回復  更多評論
            
          # re: BaseAction的java多態思考
          2007-05-25 12:10 | cheng
          真的,領教了,正在學習中!:P  回復  更多評論
            
          # re: BaseAction的java多態思考
          2007-05-25 14:32 | 勇敢的永
          樓主的思想不錯!
          學習了,謝謝樓主!  回復  更多評論
            
          # re: BaseAction的java多態思考
          2007-05-25 16:55 | 千山鳥飛絕
          我這里見過基本將struts重寫的basicAction。
          其實權限管理做得好的化,不需要basicAction。  回復  更多評論
            
          # re: BaseAction的java多態思考
          2007-05-25 16:58 | 龍卷風驛站
          為什么不繼承dispathcaction呢

          權限為什么不用spring的aop來操作呢  回復  更多評論
            
          # re: BaseAction的java多態思考
          2007-05-25 19:18 | cheng
          關于spring的權限處理~~以前聽朋友說到過~
          但具體的配置的話還要去查看下資料學習下~~呵呵  回復  更多評論
            
          # re: BaseAction的java多態思考[未登錄]
          2007-05-25 23:20 | bluesky
          我更希望無侵入式的框架,不繼承  回復  更多評論
            
          # re: BaseAction的java多態思考
          2009-04-05 17:00 | 仕途得意
          good!very  回復  更多評論
            
          主站蜘蛛池模板: 志丹县| 宽城| 谷城县| 八宿县| 海丰县| 尼木县| 闻喜县| 昂仁县| 韶关市| 望谟县| 岳池县| 海林市| 东丰县| 咸阳市| 南开区| 曲靖市| 安图县| 东方市| 封丘县| 南郑县| 双桥区| 赤水市| 嘉义市| 通州市| 彩票| 天柱县| 枣阳市| 来宾市| 盱眙县| 阿鲁科尔沁旗| 静安区| 会昌县| 田东县| 高淳县| 遂宁市| 错那县| 崇文区| 洛阳市| 会宁县| 远安县| 阿荣旗|