OMG,到底在尋找什么..................
          (構(gòu)造一個(gè)完美的J2EE系統(tǒng)所需要的完整知識體系)
          posts - 198,  comments - 37,  trackbacks - 0
          原貼地址:http://dev.yesky.com/47/2412047_1.shtml
          《實(shí)施篇》

            本篇主要介紹該平臺的具體實(shí)現(xiàn)過程。根據(jù)軟件工程的相關(guān)理論,結(jié)合筆者多年的開發(fā)經(jīng)驗(yàn),網(wǎng)站開發(fā)一般尊循以下六步驟:

            1. 收集、整理網(wǎng)站需求。

            2. 根據(jù)網(wǎng)站需求,構(gòu)想網(wǎng)頁的交互情景(即USE CASE),并設(shè)計(jì)出網(wǎng)站的原形(Prototype)。

            3. 設(shè)計(jì)出實(shí)例化對象以及后臺數(shù)據(jù)庫結(jié)構(gòu)。

            4. 采用ORM工具,建立實(shí)例化對象與后臺數(shù)據(jù)庫之間的映射關(guān)系。

            5. 根據(jù)網(wǎng)站交互需求,定制后臺Action,以處理用戶動作。

            6. 修改網(wǎng)站原形(Prototype)為動態(tài)頁面(JSP文件),將Action處理結(jié)果嵌入到動態(tài)頁面中返回給客戶端。

            在這六個(gè)步驟中,第一步實(shí)際已經(jīng)在《準(zhǔn)備篇》里已經(jīng)給出了,下面重點(diǎn)講解后面幾個(gè)步驟。

            1. 網(wǎng)站原形(Prototype)

            網(wǎng)站原形是對一個(gè)網(wǎng)站功能的頁面級描述,即看到網(wǎng)站原形就好比看到一個(gè)真實(shí)的網(wǎng)站一樣,只是網(wǎng)站原形并沒有嵌入動態(tài)代碼,而且頁面之間也缺乏關(guān)聯(lián)而已。

            網(wǎng)站原形的開發(fā)為純靜態(tài)頁面的開發(fā),制作網(wǎng)站原形的關(guān)鍵在于將網(wǎng)站功能需求轉(zhuǎn)化為人機(jī)界面。

            如易網(wǎng)的網(wǎng)站原形制作下載地址:http://www.routease.com/download/ruyinew924.rar

            2. OOP設(shè)計(jì)與后臺數(shù)據(jù)庫設(shè)計(jì)

            借助強(qiáng)大的ORM開發(fā)工具,可以將OOP與數(shù)據(jù)庫的設(shè)計(jì)同時(shí)進(jìn)行(即可以同時(shí)實(shí)施上面步驟的3,4步),這也是ORM工具最大特點(diǎn)。本項(xiàng)目采用Oracle公司的Toplink作為ORM開發(fā)工具。以下簡要介紹Toplink開發(fā)過程。

            1) 打開Toplink的Mapping Workbench組件,然后新建一個(gè)Mapping 工程。

            2) 配置工程的屬性,即在"選項(xiàng)"面板上設(shè)置工程路徑以及Java對象源代碼的路徑。

            3) 配置數(shù)據(jù)庫登陸參數(shù),包括應(yīng)用訪問數(shù)據(jù)庫的URL、用戶名、密碼等。

            完成以上三步,就可以根據(jù)應(yīng)用的需求來開發(fā)Java類。在Mapping Workbench里新建一個(gè)描述符(實(shí)際就是有一個(gè)Java類),根據(jù)需求來添加屬性,并自動生成Set/Get方法。一旦完成Java類的開發(fā)后,選擇"自動映射到數(shù)據(jù)庫"的選項(xiàng),即可實(shí)現(xiàn)數(shù)據(jù)庫表的自動創(chuàng)建。(Toplink的最大優(yōu)勢就是在定制好Java類之后可以自動生成數(shù)據(jù)庫的表結(jié)構(gòu))。

            鑒于國內(nèi)Toplink方面的資料較少,這里介紹一下Toplink生成的工程文件RouteaseMappingProject,該工程文件在web服務(wù)器啟動的時(shí)候裝載,可以理解為客戶程序?qū)?shù)據(jù)庫訪問的接口程序,他有三類方法:

            ·構(gòu)造函數(shù)

            主要是調(diào)用oracle.toplink.sessions.Project的addDescriptor方法,其作用是將數(shù)據(jù)庫和Java對象之間的映射關(guān)系加入到Project 中。代碼示范如下:

          public RouteaseMappingProject() {
          addDescriptor(buildAccountDescriptor());
          addDescriptor(buildPhoneDescriptor());
          …….
          }

            ·applyLogin方法

            它處理客戶程序登陸數(shù)據(jù),并配置一些存取數(shù)據(jù)庫的參數(shù),比如緩沖池等。代碼示范為:

          public void applyLogin() {
           //配置數(shù)據(jù)庫訪問參數(shù)
           DatabaseLogin login = new DatabaseLogin();
           login.usePlatform(new oracle.toplink.oraclespecific.Oracle9Platform());
           login.setDriverClassName("oracle.jdbc.driver.OracleDriver");  login.setConnectionString(ApplicationConfiguration.get(ConfigurationConstants.DB_CON_STR));  login.setUserName(ApplicationConfiguration.get(ConfigurationConstants.DB_USER));   login.setPassword(ApplicationConfiguration.get(ConfigurationConstants.DB_ENCRYPTED_PASSWORD));
           // 設(shè)置數(shù)據(jù)庫參數(shù)
           login.setUsesNativeSequencing(true);
           login.setSequencePreallocationSize(1);
           login.setShouldBindAllParameters(false);
           login.setShouldCacheAllStatements(false);
           login.setUsesByteArrayBinding(true);
           login.setUsesStringBinding(false);
           if (login.shouldUseByteArrayBinding()) { // Can only be used with binding.
            login.setUsesStreamsForBinding(false);
           }
           login.setShouldForceFieldNamesToUpperCase(false);
           login.setShouldOptimizeDataConversion(true);
           login.setShouldTrimStrings(true);
           login.setUsesBatchWriting(false);
           if (login.shouldUseBatchWriting()) { // Can only be used with batch writing.
            login.setUsesJDBCBatchWriting(true);
           }
           login.setUsesExternalConnectionPooling(false);
           login.setUsesExternalTransactionController(false);
           setLogin(login);
          }

            ·建立映射關(guān)系

            Toplink通過類似于builXXXDescriptor方法來建立Java對象與數(shù)據(jù)庫表字段之間的對應(yīng)關(guān)系,示范代碼如下:

          public Descriptor buildAccountDescriptor() {
           Descriptor descriptor = new Descriptor();
           descriptor.descriptorIsAggregate();
           descriptor.setJavaClass(com.routease.db.vo.user.Account.class);
           descriptor.setAlias("Account");
           // Mappings.
           //建立Account 對象的deposit屬性與數(shù)據(jù)庫表的DEPOSIT字段的對應(yīng)關(guān)系
           DirectToFieldMapping depositMapping = new DirectToFieldMapping();
           depositMapping.setAttributeName("deposit");
           depositMapping.setFieldName("DEPOSIT");
           descriptor.addMapping(depositMapping);
           …
           return descriptor;
          }

           3. 定制后臺Action

            根據(jù)MVC的精神,View和Model設(shè)計(jì)好之后應(yīng)該是將開發(fā)重點(diǎn)轉(zhuǎn)移到控制器的開發(fā)上。控制器是根據(jù)用戶行為進(jìn)行響應(yīng)的處理模塊,比如用戶通過首頁的搜索條對服務(wù)信息進(jìn)行檢索,這時(shí),web服務(wù)中的SearchToTradeEntityAction(對應(yīng)SearchToTradeEntityAction.java文件)會對用戶這一動作進(jìn)行處理。以下對這一Action進(jìn)行詳細(xì)分析:

          package com.routease.action.totradeentity;
          import java.util.Collection;
          import javax.servlet.http.HttpServletRequest;
          import javax.servlet.http.HttpServletResponse;
          import org.apache.commons.beanutils.PropertyUtils;
          import org.apache.commons.lang.StringUtils;
          import org.apache.struts.action.ActionForm;
          import org.apache.struts.action.ActionForward;
          import org.apache.struts.action.ActionMapping;
          import com.routease.action.PagingAction;
          import com.routease.action.helper.UserHelper;
          import com.routease.db.dao.DataSource;
          import com.routease.db.dao.totradeentity.SearchingCriteria;
          import com.routease.db.dao.totradeentity.ToTradeEntityDAO;
          import com.routease.db.util.Constants;
          import com.routease.db.util.Page;
          public class SearchToTradeEntityAction extends PagingAction {
           public SearchToTradeEntityAction()
           {
            super();
           }
           // executeWithDataSource方法為該Action默認(rèn)執(zhí)行的方法
           public ActionForward executeWithDataSource(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response, DataSource ds) throws Exception {
            //首先接受用戶提交的表單數(shù)據(jù)
            String objective = (String) PropertyUtils.getSimpleProperty(actionForm, "objective");
            String keyWords = (String) PropertyUtils.getSimpleProperty(actionForm, "keyWords");
            String keyWordsRange = (String) PropertyUtils.getSimpleProperty(actionForm, "keyWordsRange");
            if (StringUtils.isEmpty(keyWordsRange)) {
             keyWordsRange = SearchingCriteria.KEY_WORDS_RANGE_NAME;
            }
            String industryLevel1 = (String) PropertyUtils.getSimpleProperty(actionForm, "industryLevel1");
            String industryLevel2 = (String) PropertyUtils.getSimpleProperty(actionForm, "industryLevel2");
            String startingPrice = (String) PropertyUtils.getSimpleProperty(actionForm, "startingPrice");
            String endingPrice = (String) PropertyUtils.getSimpleProperty(actionForm, "endingPrice");
            String city = (String) PropertyUtils.getSimpleProperty(actionForm, "city");
            String province = (String) PropertyUtils.getSimpleProperty(actionForm, "province");
            String startNoStr = (String) PropertyUtils.getSimpleProperty(actionForm, "startNumber");
            String lengthStr = (String) PropertyUtils.getSimpleProperty(actionForm, "length");
            if (StringUtils.isEmpty(startNoStr)) {
             startNoStr = "1";
            }
            //根據(jù)用戶提交的數(shù)據(jù),創(chuàng)建查詢表達(dá)式對象SC
            int startNumber = Integer.parseInt(startNoStr);
            int length = UserHelper.getPagingLength(ds, request);
            ToTradeEntityDAO serviceDAO = new ToTradeEntityDAO(ds);
            SearchingCriteria sc = new SearchingCriteria();
            sc.setCity(city);
            sc.setProvince(province);
            sc.setEndingPrice(endingPrice);
            sc.setIndustryLevel1(industryLevel1);
            sc.setIndustryLevel2(industryLevel2);
            sc.setKeyWords(keyWords);
            sc.setKeyWordsRange(keyWordsRange);
            sc.setObjective(objective);
            sc.setStartingPrice(startingPrice);
            if (Constants.IS_TEST) {
             System.out.println("start of page:" + startNumber);
            }
            //提交查詢對象SC,并獲得查詢結(jié)果集,將其結(jié)果集放入Request對象中,便于返回
            Page result = serviceDAO.searchToTradeEntities(sc, startNumber, length);
            Collection industries = serviceDAO.findIndustryDistribution(sc);
            result.setSizePerPage(length);
            request.setAttribute(Constants.TO_TRADE_ENTITY, result);
            request.setAttribute("MY_INDUSTRIES",industries);
            request.setAttribute("MY_PAGE", result);
            //業(yè)務(wù)邏輯處理完畢之后,返回成功頁面
            return actionMapping.findForward("SUCCESS_PAGE");
           }
          }

            SearchToTradeEntityAction是一個(gè)典型的Action,由前面注解不難看出,一般Action分為三部分:

            a. 接受用戶表單數(shù)據(jù)

            b. 處理用戶表單數(shù)據(jù)

            c. 返回處理結(jié)果及頁面

            4. 修改頁面為JSP文件

            凡是涉及到與用戶狀態(tài)相關(guān)的頁面均應(yīng)改造為動態(tài)頁面(JSP文件),改造是在前面靜態(tài)文件的基礎(chǔ)上進(jìn)行的,用服務(wù)器端返回的數(shù)據(jù)(存放在Request對象里)替換靜態(tài)文本,由于這部分相對技術(shù)性不強(qiáng),所以不再詳細(xì)贅述了。

            通過前面四部分的介紹,基本概述了如易網(wǎng)技術(shù)實(shí)施的主要過程,在下面的一章里介紹網(wǎng)站技術(shù)中的幾個(gè)重要技巧。
          posted on 2006-05-31 21:25 OMG 閱讀(269) 評論(0)  編輯  收藏 所屬分類: 電子商務(wù)開發(fā)

          <2006年5月>
          30123456
          78910111213
          14151617181920
          21222324252627
          28293031123
          45678910

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          IT風(fēng)云人物

          文檔

          朋友

          相冊

          經(jīng)典網(wǎng)站

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 阳东县| 郧西县| 台前县| 伊春市| 吐鲁番市| 胶南市| 丹巴县| 梧州市| 博客| 陆良县| 阿克陶县| 辉南县| 镇安县| 乌兰县| 神农架林区| 西宁市| 广德县| 蛟河市| 东方市| 江永县| 社会| 安达市| 安塞县| 辽阳市| 临武县| 峨山| 买车| 诸城市| 邛崃市| 射洪县| 工布江达县| 临汾市| 北宁市| 太仓市| 漳平市| 启东市| 南京市| 秀山| 青川县| 安远县| 张家口市|