我的隱式生活(My Implicit Life)

          繼續(xù)搞“對(duì)象”,玩OO.

          首頁 新隨筆 聯(lián)系 聚合 管理
            11 Posts :: 1 Stories :: 39 Comments :: 0 Trackbacks

              前段時(shí)間,實(shí)驗(yàn)室接了學(xué)校研究生院的MIS系統(tǒng)。

                采用DelphiCSJ2EEBS結(jié)合的構(gòu)架方式。數(shù)據(jù)庫用的是師兄最熟悉的Oracle。說道Oracle,對(duì)我來說,基本使用沒有問題了,只是數(shù)據(jù)建模我還沒有主導(dǎo)設(shè)計(jì)過,心里還是沒有底。

                按照分配,我還是主導(dǎo)BS的設(shè)計(jì),更具體的說就是完成一個(gè)Java Web Application,再具體一點(diǎn)就是搞搞jspservlettagjsbeans。呵呵,技術(shù)很簡單,以前也做過了。

                但是,由于這回小組的Leader是自己,我的自由度更大了一些。能發(fā)揮什么呢,第一個(gè)靈感還是“模式”二字。那就用用吧。但肯定是需求是第一位的,沒有需要而亂用或硬用會(huì)制造無端的復(fù)雜度,讓產(chǎn)品遠(yuǎn)離需求。所以,一定要根據(jù)實(shí)際需要來用模式。

                除了我們實(shí)驗(yàn)室外,由于這個(gè)項(xiàng)目的特殊性,必須要聯(lián)合別的老師的4個(gè)小組一起做。我們導(dǎo)師是大Leader,所以,BSCS的設(shè)計(jì)規(guī)范都是我們定,一定要保證大框架的可集成性,公共資源的單一性。

                說到Java Web Application的開發(fā),以下這本書(Art of Java Web Development)不得不說。反正無論對(duì)錯(cuò),它是我的教科書。

                    
                  
              
              
             想了解這本書詳細(xì)的信息,請(qǐng)點(diǎn)擊
          這里

                 它從演化(evolution)的角度,一步一步的告訴你,Java Web Application是怎么一步一步演變過來的。說實(shí)話,我也就看了這書的前半部分。也就是主要的演變史,而后半部分的設(shè)計(jì)模式的高階應(yīng)用的例子沒有“所謂的時(shí)間”看了。呵呵。

                 談到Java Web Application的開發(fā),最基本的一個(gè)模式(跟“設(shè)計(jì)模式”的“模式”有一定的區(qū)別)就是MVC模式,而MVC最開始就是為了規(guī)范Desktop UI Application的開發(fā)而提出的。如果要跟Web Application結(jié)合的更緊一些。它就有另外一個(gè)形式,應(yīng)此便有了Model 2的由來。

                 因此,下面我們經(jīng)常說的Model 2的全稱應(yīng)該是MVC Model 2


                 







              
              這就是所謂的Model 2的說明圖。可以說,一切就這個(gè)圖完美的表示了,畢竟也不是什么難以理解的東西。詳細(xì)解釋,我沒書上說的好,也懶得說,就copy過來。

          Designers looked at MVC and modified it to work within this new development paradigm. This work led to what is now popularly called “Model 2” (to distinguish it from the desktop-centric MVC). Model 2 doesn’t change the definition of MVC; it just casts it in terms of web development. In Model 2 for Java web applications, JavaBeans represent the model. Notice that this may include simple JavaBeans, Enterprise JavaBeans (EJBs), or JavaBeans that act as proxies for EJBs. The view is rendered with JSP, which makes sense because JSP is closely tied to HTML. The controller is a servlet, well suited to executing Java code. This plays to the strengths of servlets, utilizing the services of the servlet container for lifecycle and invocation without forcing servlets to generate mixed Java code and HTML.

           

          The typical Model 2 scenario is shown in figure 1.2. The user invokes a controller servlet (more about this design later). The servlet instantiates one or more JavaBeans that perform work. The servlet then adds the bean(s) to one of the JSP collections and forwards control to a JSP. The JSP extracts the JavaBeans and displays the results.

           

                  Model 2可以說是以下Nframework的原型。很多framework都是緊密基于這個(gè)model 2模式而建立的。

                  許多開發(fā)小組,在開發(fā)Java Web Application的時(shí)候,都是直接選擇在各種framework的基礎(chǔ)上進(jìn)行開發(fā)的。下面列出一些常用的frameworks

           

          Framework

          Download from

          Description

          Struts

          http://jakarta.apache.org/struts

          A lightweight, open–source framework primarily designed for building Model 2 applications.

          Velocity

          http://jakarta.apache.org/velocity

          A Java-based template engine. Velocity permits anyone to use the simple yet powerful template language to reference objects defined in Java code.

          Tapestry

          http://jakarta.apache.org/tapestry

          A framework that is positioned primarily as an alternative to JavaServer Pages. It replaces the scripting and code generation of JSPs with a full-fledged component object model.

          WebWork

          http://sourceforge.net/projects/opensymphony

          A community project conducted using the open-source process, aimed at providing tools and a framework for building complex web sites in a short amount of time that are easy to understand and maintain.

          Turbine

          http://jakarta.apache.org/turbine

          A large, open-source, services-based framework for building extensive web applications such as e-commerce sites.

           

                  而最最常用的framework就是大名鼎鼎的,也是我唯一用過的,當(dāng)然就是struts啦。它的特點(diǎn)有:

          n         A controller servlet that dispatches requests to appropriate action classes provided by the application developer

          n         JSP custom tag libraries and associated support in the controller servlet that assists developers in creating interactive form-based applications

          n         Utility classes that support XML parsing, automatic population of JavaBeans properties based on the Java reflection APIs, and internationalization of prompts and messages

           

                  呵呵,最后我們項(xiàng)目沒有使用這里任何的一個(gè)framework,一是覺得成本高,5個(gè)小組都沒有什么接觸過的人,要搞懂一個(gè)framework再開發(fā),對(duì)于學(xué)校的同學(xué)來說,是不現(xiàn)實(shí)的。二是實(shí)用性也不強(qiáng),struts中的很多功能,我們這里并不需要。

                  結(jié)論是,咱自己設(shè)計(jì)了另一個(gè)基于model 2framework,當(dāng)然,稱為framework真不好意思。但是,想來想去,自己這套東西,真的有可以重用的東西,的確可以實(shí)現(xiàn)一些“genetic”的功能。暫且就認(rèn)為是一種幼稚的framework吧。由于是給研究生院(graduate school)做的,就叫gs框架吧。雖然功能簡陋,但也有它弱智的能力,其由以下一些類和支持元素組成:

          1.         一個(gè)基類servletGSServlet。類圖如下:



                   



                   
                   
                   gs
          框架下的servlet都要繼承于它。

                            a)         子類要override以下的方法:

              n         validate,控制此servlet能被誰訪問的代碼。其實(shí)內(nèi)容就是初始化屬性validator

              n         process,此servlet邏輯業(yè)務(wù)代碼,以前寫在doPostdoGet中的代碼。

                            b)         子類可以使用的方法:

              n         forwardErr,出錯(cuò)跳轉(zhuǎn)方法,當(dāng)然根據(jù)不同的出錯(cuò)參數(shù),有不同的出錯(cuò)顯示。

              n         forward,跳轉(zhuǎn)下一個(gè)頁面的方法。

              n         forwardSuccess,有了forward,此方法舍棄了。

              n         forwardWithObject,帶著對(duì)象跳轉(zhuǎn)頁面的方法。













          2.        
          一個(gè)對(duì)用戶驗(yàn)證的框架(如下圖),每個(gè)
          servlet都會(huì)用此驗(yàn)證框架來指定可訪問用戶的身份。


                 這個(gè)框架可重用的部分為接口Validator抽象類Role兩個(gè)關(guān)系類OrRelationAndRelation
              而繼承于
          Role的各個(gè)具體角色則要根據(jù)不同的項(xiàng)目的實(shí)際情況自行定義,gs項(xiàng)目中暫時(shí)只有三種角色:游客,學(xué)生,教師。對(duì)于這個(gè)的使用,后面會(huì)說到的。明眼人一看就可以知道這里用了裝飾模式,我自豪啊。

          3.         JSTL,這里使用了JSTL作為頁面的標(biāo)準(zhǔn)標(biāo)記。

                 在頁面中的條件判斷和迭代,就都靠它了。相對(duì)于自定義標(biāo)記,使用JSTL作為jsp標(biāo)記,我個(gè)人認(rèn)為是性價(jià)比非常好的一種選擇,畢竟學(xué)習(xí)自定義標(biāo)記的開發(fā)要一定的時(shí)間,而且一但使用,量一定很大。

          4.         tipErr.jsp,顯示出錯(cuò)信息頁面。

                 出錯(cuò)提示對(duì)于一個(gè)framework肯定是必要的,既然GSServlet中有通用的跳轉(zhuǎn)出錯(cuò)頁面的方法,那也就把這個(gè)顯示出錯(cuò)信息頁面當(dāng)作框架的一部分吧,不妥之處請(qǐng)見諒。


                
          由上面的四點(diǎn)構(gòu)成了本項(xiàng)目的一個(gè)簡易framework

              下面是基于這種framework開發(fā)的一個(gè)servlet例子。其功能是為教師和學(xué)生提供成績查詢。類圖如下:

          框架代碼:


          首先,必須
          Override
          父類的process方法,可見,本servlet的業(yè)務(wù)邏輯都在這里。

           1protected void process(HttpServletRequest req, HttpServletResponse resp)
           2        throws ServletException, IOException {
           3
           4        req.setCharacterEncoding("GB2312");
           5
           6        String function = req.getParameter("function");
           7
           8        if (function.equals("getQueryType_Stu")) {
           9            this.queryNullQueryScoreStu(req, resp);
          10        }
           else if (function.equals("getQueryType_Tch")) {
          11            this.queryNullQueryScoreTch(req, resp);
          12        }
           else if (function.equalsIgnoreCase("queryScoreStu")) {
          13            this.queryScoreStu(req, resp);
          14        }
           else if (function.equalsIgnoreCase("queryScoreTch")) {
          15            this.queryScoreTch(req, resp);
          16        }
           else {
          17            this.forwardErr(req, resp, "頁面沒傳參數(shù)進(jìn)來");
          18        }

          19    }

          20

                
          接著,必須Override父類的validate方法,實(shí)現(xiàn)對(duì)本servlet的訪問控制。

          1protected void validate(HttpServletRequest req) {
          2        //教師和學(xué)生都可以。
          3        validator = new Or_Teacher(new Or_Student(new OrRelation(req)));
          4    }

          5

          再者,有多少子邏輯功能,就寫多少個(gè)下面的業(yè)務(wù)方法。父類GSServlet的一些跳轉(zhuǎn)方法這里就可以用了。

           1   /**
           2     * 學(xué)生查詢成績
           3     * @param req
           4     * @param resp
           5     */

           6    private void queryScoreStu(
           7        HttpServletRequest req,
           8        HttpServletResponse resp)
           9        throws ServletException, IOException {
          10
          11        //1:獲得用戶ID
          12        HttpSession session = req.getSession(false);
          13        Ent_BaseUser user = (Ent_BaseUser) session.getAttribute("USER");
          14        String stuID = user.getId();
          15
          16        //2:獲得查詢類型
          17        String queryType = req.getParameter("queryType");
          18
          19        //3:準(zhǔn)備存放查出成績對(duì)象的集合
          20        ArrayList scores = null;
          21
          22        //4:準(zhǔn)備要使用的工廠對(duì)象
          23        Bean_Score bs = new Bean_Score();
          24
          25        //5:判斷查詢類型(單課頭,全部課頭)
          26        if (queryType.equals("0")) //0--查單課頭
          27            //獲得查詢課頭ID
          28            String lessonHeadID = req.getParameter("lessonHeadID");
          29            //得到結(jié)果
          30            try {
          31                scores = bs.findByStuIDLessonHeadID(stuID, lessonHeadID);
          32            }
           catch (Exception e) {
          33                e.printStackTrace();
          34                this.forwardErr(req, resp, DB_ERROR);
          35            }

          36        }
           else //1--查全部課頭
          37            try {
          38                scores = bs.findAllScoreByStuID(stuID);
          39            }
           catch (Exception e) {
          40                e.printStackTrace();
          41                this.forwardErr(req, resp, DB_ERROR);
          42            }

          43        }

          44        //6:搞定就forward給頁面去顯示!
          45        this.forwardWithObject(
          46            req,
          47            resp,
          48            "../gs.train/stu/score/scoreStu.jsp",
          49            "scoreResults",
          50            scores);
          51        //this.forward(req,resp,"../common/viewNotice.jsp");
          52
          53    }

          54
          55    /** 
          56     * 老師查詢成績
          57     * @param req
          58     * @param resp
          59     */

          60    private void queryScoreTch(
          61        HttpServletRequest req,
          62        HttpServletResponse resp)
          63        throws ServletException, IOException {
          64        //省略……………………
          65    }

          66    
          67    //省略……………………
          68

                  顯示的jsp頁面就用JSTLforward過來的東西顯示出來,一個(gè)業(yè)務(wù)過程就完成了。

                  這個(gè)簡單的framework在本項(xiàng)目中基本符合使用要求,特別是別的小組的同學(xué)不需要花太多的時(shí)間就能很好的使用,希望以后實(shí)驗(yàn)室能夠考慮重用這個(gè)東西,呵呵。

                                                                MARCO ZHANG 2006年2月10日5:10:08

          posted on 2006-02-10 05:48 marco 閱讀(2042) 評(píng)論(1)  編輯  收藏 所屬分類: -=Design Pattern=--=Java Techs=--=Project Experience=-

          Feedback

          # re: Java Web Application開發(fā)日志之一--自己設(shè)計(jì)簡易framework 2006-02-16 09:25 飛翔的西瓜
          呵呵,很詳細(xì)哦  回復(fù)  更多評(píng)論
            

          主站蜘蛛池模板: 凤凰县| 泰来县| 灵宝市| 邳州市| 阿克陶县| 德清县| 枣庄市| 盖州市| 呼和浩特市| 韶关市| 松江区| 通州区| 卢氏县| 宾川县| 南乐县| 韩城市| 兰考县| 泽州县| 郓城县| 赞皇县| 灌南县| 上思县| 阿克陶县| 定安县| 青铜峡市| 高唐县| 兴宁市| 万源市| 大英县| 普定县| 武冈市| 贵港市| 岳阳市| 建瓯市| 方山县| 丰原市| 中牟县| 通化市| 庆城县| 汉川市| 彩票|