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

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

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

    隨筆-39  評(píng)論-33  文章-0  trackbacks-0

    DAO類+通用持久類+通用動(dòng)態(tài)formBean類+通用DispatchAction類,實(shí)現(xiàn)數(shù)據(jù)增、刪、改、查

    newxy(新坐標(biāo)) 技術(shù)運(yùn)用之四

    DAO 類: net.newxy.dbm.DBM 及其子類

    通用持久類: net.newxy.dbm.DynaDto

    通用動(dòng)態(tài) formBean 類: net.newxy.struts_faces.DynaFormBean

    通用 DispatchAction 類: net.newxy.struts_faces.DispatchAction

    在《 DAO + 通用持久類 + 通用動(dòng)態(tài) formBean 類,實(shí)現(xiàn)數(shù)據(jù)增、刪、改、查》(又名《web開發(fā):通用持久類代替hibernate的持久類、通用動(dòng)態(tài)formBean類代替struts的formBean類》)文章中已介紹了 DAO 類、通用持久類、通用動(dòng)態(tài) formBean 類在數(shù)據(jù)增、刪、改、 查中的運(yùn)用。本篇增加了一個(gè)類net.newxy.struts_faces.DispatchAction,目的是為開發(fā)者提供幾個(gè)通用的DispatchAction方法,節(jié)省代碼,增加開發(fā)效率。

    一、?????? net.newxy.struts_faces.DispatchAction類圖

    net.newxy.struts_faces.DispatchAction繼承自struts的 org.apache.struts.actions.DispatchAction

    二、net.newxy.struts_faces.DispatchAction的重要方法

    ? 1 getActionForward

    ??? public ActionForward getActionForward(ActionMapping actionMapping,HttpServletRequest httpServletRequest){

    ????? String frwd=httpServletRequest.getParameter("_frwd");

    ????? if(frwd!=null && frwd.trim().length()>0)

    ??????? return actionMapping.findForward(frwd);

    ????? else{

    ??????? if(actionMapping.getInputForward().getPath()!=null)

    ??????? ??return actionMapping.getInputForward();

    ??????? else{

    ??????????? httpServletRequest.setAttribute("message",net.newxy.cfg.Message.getMessage("errors.path")+";please give a forward for action");

    ??????????? return actionMapping.findForward("error");

    ???? ???}

    ????? }

    ??? }

    _frwd 參數(shù)值作為 action 子屬性 <forward /> name 值.如果此值不空,以其為 forward name 值轉(zhuǎn)發(fā),如果沒有此參數(shù)或其值為空, actionMapping 會(huì)查看 action input 參數(shù),如果有 input forward ,則轉(zhuǎn)到 input forward ,否則將"沒有轉(zhuǎn)發(fā)路徑"類的信息保存到 request 中,并以名為 ”error” 的全局 forward 進(jìn)行轉(zhuǎn)發(fā).

    2 find

    jsp 頁面表單傳來的數(shù)據(jù)在這里組合成 sql 查詢語句,進(jìn)行查詢,將查詢結(jié)果保存到 actionForm _coll 屬性中。

    public ActionForward find(ActionMapping actionMapping, ActionForm actionForm,

    HttpServletRequest httpServletRequest,

    HttpServletResponse httpServletResponse) throws Exception{

    ......

    return getActionForward(actionMapping,httpServletRequest);

    }

    actionForm net.newxy.struts_faces.FormBean 類型, net.newxy.struts_faces.DynaFormBean的父類

    如果發(fā)生異常,將異常信息以“ message” 為名保存到 request 中,并以“ error” 為全局 forward name 值進(jìn)行轉(zhuǎn)發(fā):

    ?????? httpServletRequest.setAttribute("error",e.getMessage());

    ?????? return actionMapping.findForward("error");

    全局 forward 定義舉例如下:

    ? <global-forwards>

    ??? <forward name="error" path="/error.jsp" />

    ? </global-forwards>

    在“ /error.jsp ”頁面上可用下列代碼顯示異常:

    <logic:present name="message" scope="request">

    ? <bean:write name=”message” scope=”request”/>

    </logic:present>

    如果數(shù)據(jù)查詢過程沒有異常,執(zhí)行 return getActionForward(actionMapping,httpServletRequest) 語句轉(zhuǎn)發(fā)。

    ?

    3 update

    頁面上傳的數(shù)據(jù)綁定到 formBean 中, Object dto=form.getDto()方法得到包含上傳數(shù)據(jù)的持久類對(duì)象,以該對(duì)象為參數(shù)調(diào)用DAO類的update(Object dto)方法,作更新或插入操作。

    public ActionForward update(ActionMapping actionMapping, ActionForm actionForm,

    ????????????????????????????? HttpServletRequest httpServletRequest,

    ??????????? HttpServletResponse httpServletResponse) throws Exception{

    ??? ......

    ??? net.newxy.struts_faces.FormBean form=(net.newxy.struts_faces.FormBean)actionForm;

    try{

    Object dto=form.getDto();

    IFacade ifacade=IFacadeFactory.getFacade(httpServletRequest.getParameter("_dao"), httpServletRequest.getLocale());

    ?????? Object result=ifacade.update(dto);

    ?????? ......

    ??? }catch(Exception e){

    ??????? ......

    }

    ......

    return getActionForward(actionMapping,httpServletRequest);

    }

    actionForm net.newxy.struts_faces.FormBean 類型, net.newxy.struts_faces.DynaFormBean的父類。

    IFacadeFactory是DAO類的抽象工廠類。

    IFacadeFactory.getFacade(String _dao,java.util.Locale locale)方法返回DAO類接口net.newxy.dbm.IFacade。

    異常處理與轉(zhuǎn)發(fā)和find方法相同。

    ?

    4、remove

    如果httpServletRequest.getParameter("_index")不空,刪除參數(shù)值指定的那條記錄,否則刪除formBean數(shù)據(jù)對(duì)應(yīng)的那條記錄。

    public ActionForward remove(ActionMapping actionMapping, ActionForm actionForm,

    ??????? ??????????????????????HttpServletRequest httpServletRequest,

    ??????????? HttpServletResponse httpServletResponse) throws Exception{

    ......

    String index=httpServletRequest.getParameter("_index");

    try{

    IFacade ifacade=IFacadeFactory.getFacade(httpServletRequest.getParameter("_dao"), httpServletRequest.getLocale());

    ??? if (index != null) {

    ??????? net.newxy.util.FormBeanUtils.remove(ifacade, form, index);

    ??? } else

    ??????? net.newxy.util.FormBeanUtils.remove(ifacade, form);

    ??? }catch(Exception e){

    ??????? httpServletRequest.setAttribute("error",e.getMessage());

    ??????? return actionMapping.findForward("error");

    ??? }

    ......

    return getActionForward(actionMapping,httpServletRequest);

    }

    actionForm net.newxy.struts_faces.FormBean 類型, net.newxy.struts_faces.DynaFormBean的父類。

    IFacadeFactory是DAO類的抽象工廠類。

    IFacadeFactory.getFacade(String _dao,java.util.Locale locale)方法返回DAO類接口net.newxy.dbm.IFacade。

    異常處理與轉(zhuǎn)發(fā)和find方法相同。

    ?

    5、edit

    edit方法從httpServletRequest.getParameter("_index")得到記錄索引號(hào),找到這條記錄,將其數(shù)據(jù)填到actionForm中。

    "edit"名意上是“編輯”,實(shí)際是將某條記錄填入formBean中,供編輯或顯示。

    public ActionForward edit(ActionMapping actionMapping, ActionForm actionForm,

    ????????????????????????????? HttpServletRequest httpServletRequest,

    ??????????? HttpServletResponse httpServletResponse) throws Exception{

    ??? ......

    ??? net.newxy.struts_faces.FormBean form=(net.newxy.struts_faces.FormBean)actionForm;

    String _index=httpServletRequest.getParameter("_index");

    if(_index==null)

    ??? _index=form.get_index();

    if(_index!=null)

    ??? form.setForm(_index);

    ......

    return getActionForward(actionMapping,httpServletRequest);

    }

    actionForm net.newxy.struts_faces.FormBean 類型, net.newxy.struts_faces.DynaFormBean的父類。

    異常處理與轉(zhuǎn)發(fā)和find方法相同。

    ?

    6、empty

    empty方法的目的是清空formBean,包括主關(guān)鍵字段屬性也被清空,返回給用戶的是空白表單,用戶編輯數(shù)據(jù),submit后,ActionServlet將數(shù)據(jù)綁定到formBean中。如果在后臺(tái)調(diào)用DAO類的update(Object dto)方法,因?yàn)榍宄酥麝P(guān)鍵字屬性,作插入操作。

    empty方法的目的之一是讓用戶得到一空白表單,目的之二是為了數(shù)據(jù)插入。

    public ActionForward empty(ActionMapping actionMapping, ActionForm actionForm,

    ????????????????????????????? HttpServletRequest httpServletRequest,

    ??????????? HttpServletResponse httpServletResponse) throws Exception{

    ??? ......

    ??? net.newxy.struts_faces.FormBean form=(net.newxy.struts_faces.FormBean)actionForm;

    ......

    form.empty();

    ......

    return getActionForward(actionMapping,httpServletRequest);

    }

    actionForm net.newxy.struts_faces.FormBean 類型, net.newxy.struts_faces.DynaFormBean的父類。

    異常處理與轉(zhuǎn)發(fā)和find方法相同。

    ?

    二、建議和約定

    建議一、將net.newxy.struts_faces.DispatchAction作為通用DispatchAction類。

    因?yàn)?span lang="EN-US">newxy(新坐標(biāo))的net.newxy.struts_faces.DispatchAction可以實(shí)現(xiàn)大部的數(shù)據(jù)增、刪、改、查。它可以作為通用DispatchActionAction類。結(jié)合通用動(dòng)態(tài)formBean類net.newxy.struts_faces.DynaFormBean,開發(fā)者在大多數(shù)情況下可以不寫代碼或少寫代碼實(shí)現(xiàn)數(shù)據(jù)增、刪、改、查

    舉例:

    兩個(gè)表:1.行業(yè)表industry,2.企業(yè)表enterprise。

    兩個(gè)formBean:1.formBeanIndustry與表industry對(duì)應(yīng),2.formBeanEnterprise與表enterprise對(duì)應(yīng)。

    兩個(gè)DispatchAction: 1.actionIndustry與formBeanIndustry關(guān)聯(lián),有兩個(gè)轉(zhuǎn)發(fā)條件,2.actionEnterprise與formBeanEnterprise關(guān)聯(lián),也有兩個(gè)轉(zhuǎn)發(fā)條件。

    Struts設(shè)置如下:

    ? <form-beans>

    ??? <form-bean name="formBeanEnterprise" type="net.newxy.struts_faces.DynaFormBean" />

    ??? <form-bean name="formBeanIndustry" type="net.newxy.struts_faces.DynaFormBean" />

    ? </form-beans>

    ? <action-mappings>

    ??? <action name="formBeanEnterprise" parameter="method" path="/actionEnterprse" scope="session" type="net.newxy.struts_faces.DispatchAction">

    ????? <forward name="forward1" path="/jsp1.jsp" />

    ????? <forward name="forward2" path="/jsp2.jsp" />

    ??? </action>

    ??? <action name="formBeanIndustry" parameter="method" path="/actionIndustry" scope="session" type="net.newxy.struts_faces.DispatchAction">

    ????? <forward name="forward3" path="/jsp3.jsp" />

    ????? <forward name="forward4" path="/jsp4.jsp" />

    ??? </action>

    ? </action-mappings>

    現(xiàn)在在 jsp1.jsp 頁面上對(duì) enterprise 表進(jìn)行編輯:

    <html:form action="/actionEnterpise.do?method=update&_frwd=forward2">

    ? 企業(yè)名稱 :<html:text property="name"></html:text><br />

    ? 企業(yè)地址 :<html:text property="address"></html:text><br />

    ? <html:submit value=" 提交 ">

    ? </html:submit>

    </html:form>

    action="/actionEnterpise.do?method=update&_frwd=forward2 "其中的 method 參數(shù)名由 actionEnterpirse parameter 值決定, update 是通用 Action net.newxy.struts_faces.DispatchAction 的一個(gè)方法 ,用于插入或更新數(shù)據(jù)。 _frwd 參數(shù)是通用動(dòng)態(tài) formBean net.newxy.struts_faces.DynaFormBean 的保留屬性 ,其值是 struts action 子屬性 <forward /> name 值。 _frwd=forward2 ,是要后臺(tái) update 數(shù)據(jù)后轉(zhuǎn)到 jsp2.jsp 頁面。

    開發(fā)者應(yīng)設(shè)置一名為 error 的全局轉(zhuǎn)發(fā):

    ? <global-forwards>

    ??? <forward name="error" path="/error.jsp" />

    ? </global-forwards>

    error.jsp 頁面上加入:

    <logic:present name="message" scope="request">

    ? <bean:write name=”message” scope=”request”/>

    </logic:present>

    如果 net.newxy.struts_faces.DispatchAction 在執(zhí)行 update 操作時(shí)發(fā)生了錯(cuò)誤,會(huì)轉(zhuǎn)到 error.jsp 頁面上來,并將錯(cuò)誤信息顯示出來.

    ?

    建議二、開發(fā)者的action類繼承net.newxy.struts_faces.DispatchAction

    在開發(fā)過程中,有時(shí)要在數(shù)據(jù)插入或更新到數(shù)據(jù)庫前從formBean中提取數(shù)據(jù),或更改formBean中的數(shù)據(jù).建議開發(fā)者的action類繼承 net.newxy.struts_faces.DispatchAction ,如:

    public class MyDispatchAction extends net.newxy.struts_faces.DispatchAction {

    public ActionForward myUpdate(ActionMapping actionMapping, ActionForm actionForm,

    ????????????????????????????? HttpServletRequest httpServletRequest,

    HttpServletResponse httpServletResponse) throws Exception{

    ……? //開發(fā)者代碼

    try{

    net.newxy.struts_faces.DynaFormBean form=(net.newxy.struts_faces.DynaFormBean)actionForm;//開發(fā)者代碼

    form.set(“field1”,”…”);//開發(fā)者代碼

    return super.update(actionMapping,actionForm,httpServletRequest,httpServletResponse);

    catch(Exception e){

    ??? httpServletRequest.setAttribute(“message”,e.getMessage()); // 錯(cuò)誤信息以 message 為名保存到 request 中.

    ??? return actionMapping.findForward(“error”); // 全局轉(zhuǎn)發(fā)

    }

    }

    }

    數(shù)據(jù)上傳到后臺(tái),開發(fā)在將數(shù)據(jù)送到數(shù)據(jù)庫前,改變了field1字段屬性值.

    ?

    約定

    約定與保留屬性有關(guān),下面是對(duì)頁面上傳的參數(shù)名的約定:

    _frwd:其值 struts action 子屬性 <forward /> name 值,用于轉(zhuǎn)發(fā) ;

    _dao DAO 類的別名;

    _table :要插入 更新或刪除數(shù)據(jù)的表名;

    _sql :查詢的基本條件.其它條件可由用戶選擇;

    _index :記錄索引號(hào),用于顯示 編輯 或刪除這條記錄.

    理解本篇的關(guān)鍵是理解 struts DispatchAction 類, newxy( 新坐標(biāo) ) net.newxy.struts_faces.DispatchAction 繼承自struts的DispatchAction類,具有相同的工作原理.struts的action設(shè)置要有parameter的值,如果此值等于"method",jsp頁面表單的action值就可以是如下形式:

        action="/actionEnterpise.do?method=myUpdate&_frwd=forward2

    myUpdate是 DispatchAction子類的一個(gè)方法.

    ?

    newxy(新坐標(biāo))技術(shù)網(wǎng)站 http://www.newxy.net

    posted on 2006-07-14 09:05 newxy新坐標(biāo) 閱讀(229) 評(píng)論(0)  編輯  收藏

    只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 国产一区二区三区在线免费| 免费在线观看亚洲| 亚洲爆乳无码专区www| 亚洲av午夜成人片精品电影| 免费人成在线观看视频高潮 | 亚洲综合在线视频| 女性无套免费网站在线看| eeuss在线兵区免费观看| 亚洲国产综合第一精品小说| 亚洲AV无码不卡在线观看下载| 国产麻豆一精品一AV一免费| 亚洲性无码一区二区三区| 国产成人亚洲影院在线观看| 97免费人妻无码视频| 成人免费夜片在线观看| 亚洲精品乱码久久久久久下载| 免费在线观看一级毛片| 亚洲综合免费视频| 一级黄色毛片免费看| 亚洲国产av一区二区三区丶| 亚洲中文字幕无码一区二区三区 | 亚洲AⅤ永久无码精品AA| 91香焦国产线观看看免费| 国产亚洲午夜精品| 亚洲综合激情九月婷婷| 超清首页国产亚洲丝袜| 成年男女免费视频网站| 久久国产精品免费网站| 午夜在线免费视频 | 亚洲综合亚洲综合网成人| 国产又黄又爽又猛免费app| 中出五十路免费视频| 亚洲国产精品99久久久久久| 亚洲福利电影在线观看| 亚洲色无码专区在线观看| 无码欧精品亚洲日韩一区夜夜嗨 | 久久久久久久91精品免费观看| 美女视频黄a视频全免费网站色窝| 国产亚洲蜜芽精品久久| 亚洲综合无码无在线观看| 亚洲视频免费一区|