<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    我的隱式生活(My Implicit Life)

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

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

        前段時間,實驗室接了學校研究生院的MIS系統(tǒng)。

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

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

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

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

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

              
            
        
        
       想了解這本書詳細的信息,請點擊
    這里

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

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

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


           







        
        這就是所謂的Model 2的說明圖。可以說,一切就這個圖完美的表示了,畢竟也不是什么難以理解的東西。詳細解釋,我沒書上說的好,也懶得說,就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都是緊密基于這個model 2模式而建立的。

            許多開發(fā)小組,在開發(fā)Java Web Application的時候,都是直接選擇在各種framework的基礎(chǔ)上進行開發(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就是大名鼎鼎的,也是我唯一用過的,當然就是struts啦。它的特點有:

    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

     

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

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

    1.         一個基類servletGSServlet。類圖如下:



             



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

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

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

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

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

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

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

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

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













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


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

    3.         JSTL,這里使用了JSTL作為頁面的標準標記。

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

    4.         tipErr.jsp,顯示出錯信息頁面。

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


          
    由上面的四點構(gòu)成了本項目的一個簡易framework

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

    框架代碼:


    首先,必須
    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ù)進來");
    18        }

    19    }

    20

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

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

    5

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

     1   /**
     2     * 學生查詢成績
     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:準備存放查出成績對象的集合
    20        ArrayList scores = null;
    21
    22        //4:準備要使用的工廠對象
    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過來的東西顯示出來,一個業(yè)務(wù)過程就完成了。

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

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

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

    Feedback

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

    主站蜘蛛池模板: 久久亚洲精品无码| 国产精品无码免费播放| 丁香花在线观看免费观看图片| 色偷偷亚洲第一综合网| 亚洲AV成人无码网天堂| 亚洲av永久中文无码精品| 亚洲精品色播一区二区| 亚洲精品色在线网站| 国产大陆亚洲精品国产| 日日摸日日碰夜夜爽亚洲| 黄色一级毛片免费| fc2成年免费共享视频18| 中文在线免费观看| 国产成人精品免费久久久久| 日韩精品久久久久久免费| 一级毛片免费观看| 亚洲黄色片免费看| 成人影片麻豆国产影片免费观看 | 久久精品国产亚洲av麻豆色欲 | 国产乱人免费视频| 亚洲情a成黄在线观看| 国产亚洲一区二区三区在线不卡 | 中文字幕亚洲无线码a| 成人午夜亚洲精品无码网站| 亚洲AV永久无码精品成人| 久久水蜜桃亚洲av无码精品麻豆| 亚洲最大中文字幕| 亚洲国产成人久久精品大牛影视| 九九久久精品国产免费看小说| 久久久久免费视频| 日韩午夜理论免费TV影院| 免费可以在线看A∨网站| 亚洲成a人片在线观看久| 久久久久亚洲AV成人无码网站| 久久精品国产亚洲av水果派 | 亚洲黄色一级毛片| 亚洲成AV人片高潮喷水| a毛片久久免费观看| 午夜国产精品免费观看| 亚洲国产成人久久一区久久| 亚洲国产成人久久精品动漫 |