posts - 188,comments - 176,trackbacks - 0

          對(duì)于struts框架,我們都知道我們寫的*Action類是繼承struts的Action(org.apache.struts.action.Action)類,并重寫其定義的execute方法,進(jìn)而來(lái)實(shí)現(xiàn)我們自己的業(yè)務(wù)邏輯。

          但考慮到到一些需求,我們可以在struts的Action和我們自己寫的*Action類之間加一層Action類來(lái)實(shí)現(xiàn)過(guò)濾功能,我們將其定義為BaseAction,整個(gè)繼承關(guān)系:*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); 
           }
           
           //實(shí)現(xiàn)BaseAction中的needLogin方法,判斷*Action是否需要登陸驗(yàn)證
           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){
               //登陸驗(yàn)證失敗,返回login.jsp
               return actionMapping.findForward("login");
             }
            } 
            //返回調(diào)用ListAction中的doExcute()方法,執(zhí)行業(yè)務(wù)邏輯
            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類,登陸系統(tǒng)是根據(jù)login.do進(jìn)入LoginAction,調(diào)用重寫struts中的excute()方法,此時(shí)LoginAction沒(méi)有,就到父類BaseAction去調(diào)用excute()方法,執(zhí)行其業(yè)務(wù)邏輯后,如過(guò)用戶是已經(jīng)登陸過(guò)就返回doExcute()方法,回到ListAction調(diào)用doExcute()的業(yè)務(wù)邏輯,如果是非登陸用戶則直接轉(zhuǎn)向Login.jsp。這里將java中多態(tài),抽象類以及回調(diào)的思想體現(xiàn)得淋漓盡致。


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

          FeedBack:
          # re: BaseAction的java多態(tài)思考
          2007-05-25 10:59 | framework
          這種方式很好的,但實(shí)際應(yīng)用時(shí)要比這個(gè)復(fù)雜, 要進(jìn)行方法分派  回復(fù)  更多評(píng)論
            
          # re: BaseAction的java多態(tài)思考
          2007-05-25 11:03 | cheng
          恩,這是一個(gè)基本的實(shí)現(xiàn),個(gè)人感覺(jué)關(guān)鍵是思想意識(shí)的形成。
          當(dāng)然具體問(wèn)題肯定就要具體去分析了~~~
          因?yàn)檫€沒(méi)有接觸到大的項(xiàng)目,所以,呵呵。目前還沒(méi)有體會(huì)到你那種意境了~~~
          只有等以后做項(xiàng)目當(dāng)中去體會(huì)和提升了~!  回復(fù)  更多評(píng)論
            
          # re: BaseAction的java多態(tài)思考
          2007-05-25 11:29 | dennis
          把權(quán)限的判斷分散到各個(gè)action中并不是個(gè)好主意,用filter可以搞定的事情沒(méi)必要這么復(fù)雜  回復(fù)  更多評(píng)論
            
          # re: BaseAction的java多態(tài)思考[未登錄](méi)
          2007-05-25 11:46 | Aqing
          用過(guò)濾器實(shí)現(xiàn)比較簡(jiǎn)單:

          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;

          /**
          * 用于檢測(cè)用戶是否登陸的過(guò)濾器,如果未登錄,則重定向到指的登錄頁(yè)面 配置參數(shù) redirectURL 如果用戶未登錄,則重定向到指定的頁(yè)面,URL不包括
          * ContextPath notCheckURLList不做檢查的URL列表,以分號(hào)分開(kāi),并且 URL 中不包括 ContextPath
          * checkSessionKey 需檢查的在 Session 中保存的關(guān)鍵字
          */

          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();
          }
          }  回復(fù)  更多評(píng)論
            
          # re: BaseAction的java多態(tài)思考
          2007-05-25 12:10 | cheng
          真的,領(lǐng)教了,正在學(xué)習(xí)中!:P  回復(fù)  更多評(píng)論
            
          # re: BaseAction的java多態(tài)思考
          2007-05-25 14:32 | 勇敢的永
          樓主的思想不錯(cuò)!
          學(xué)習(xí)了,謝謝樓主!  回復(fù)  更多評(píng)論
            
          # re: BaseAction的java多態(tài)思考
          2007-05-25 16:55 | 千山鳥飛絕
          我這里見(jiàn)過(guò)基本將struts重寫的basicAction。
          其實(shí)權(quán)限管理做得好的化,不需要basicAction。  回復(fù)  更多評(píng)論
            
          # re: BaseAction的java多態(tài)思考
          2007-05-25 16:58 | 龍卷風(fēng)驛站
          為什么不繼承dispathcaction呢

          權(quán)限為什么不用spring的aop來(lái)操作呢  回復(fù)  更多評(píng)論
            
          # re: BaseAction的java多態(tài)思考
          2007-05-25 19:18 | cheng
          關(guān)于spring的權(quán)限處理~~以前聽(tīng)朋友說(shuō)到過(guò)~
          但具體的配置的話還要去查看下資料學(xué)習(xí)下~~呵呵  回復(fù)  更多評(píng)論
            
          # re: BaseAction的java多態(tài)思考[未登錄](méi)
          2007-05-25 23:20 | bluesky
          我更希望無(wú)侵入式的框架,不繼承  回復(fù)  更多評(píng)論
            
          # re: BaseAction的java多態(tài)思考
          2009-04-05 17:00 | 仕途得意
          good!very  回復(fù)  更多評(píng)論
            
          主站蜘蛛池模板: 图木舒克市| 宜宾县| 丰原市| 井研县| 桦南县| 兴业县| 邻水| 钟祥市| 铁岭县| 长宁县| 彭阳县| 麻江县| 望奎县| 吴忠市| 南昌市| 灵武市| 建宁县| 丘北县| 调兵山市| 台南市| 新沂市| 兴城市| 宿松县| 宜良县| 大足县| 德格县| 灵台县| 子洲县| 五家渠市| 张掖市| 余庆县| 咸宁市| 江西省| 民权县| 沅陵县| 榆林市| 桂平市| 普陀区| 获嘉县| 宿州市| 阜南县|