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

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

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

    隨筆-39  評論-33  文章-0  trackbacks-0

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

    newxy(新坐標) 技術運用之四

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

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

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

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

    在《 DAO + 通用持久類 + 通用動態(tài) formBean 類,實現(xiàn)數(shù)據(jù)增、刪、改、查》(又名《web開發(fā):通用持久類代替hibernate的持久類、通用動態(tài)formBean類代替struts的formBean類》)文章中已介紹了 DAO 類、通用持久類、通用動態(tài) formBean 類在數(shù)據(jù)增、刪、改、 查中的運用。本篇增加了一個類net.newxy.struts_faces.DispatchAction,目的是為開發(fā)者提供幾個通用的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 值轉發(fā),如果沒有此參數(shù)或其值為空, actionMapping 會查看 action input 參數(shù),如果有 input forward ,則轉到 input forward ,否則將"沒有轉發(fā)路徑"類的信息保存到 request 中,并以名為 ”error” 的全局 forward 進行轉發(fā).

    2 find

    jsp 頁面表單傳來的數(shù)據(jù)在這里組合成 sql 查詢語句,進行查詢,將查詢結果保存到 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 值進行轉發(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) 語句轉發(fā)。

    ?

    3 update

    頁面上傳的數(shù)據(jù)綁定到 formBean 中, Object dto=form.getDto()方法得到包含上傳數(shù)據(jù)的持久類對象,以該對象為參數(shù)調用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。

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

    ?

    4、remove

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

    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。

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

    ?

    5、edit

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

    "edit"名意上是“編輯”,實際是將某條記錄填入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的父類。

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

    ?

    6、empty

    empty方法的目的是清空formBean,包括主關鍵字段屬性也被清空,返回給用戶的是空白表單,用戶編輯數(shù)據(jù),submit后,ActionServlet將數(shù)據(jù)綁定到formBean中。如果在后臺調用DAO類的update(Object dto)方法,因為清除了主關鍵字屬性,作插入操作。

    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的父類。

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

    ?

    二、建議和約定

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

    因為newxy(新坐標)的net.newxy.struts_faces.DispatchAction可以實現(xiàn)大部的數(shù)據(jù)增、刪、改、查。它可以作為通用DispatchActionAction類。結合通用動態(tài)formBean類net.newxy.struts_faces.DynaFormBean,開發(fā)者在大多數(shù)情況下可以不寫代碼或少寫代碼實現(xiàn)數(shù)據(jù)增、刪、改、查

    舉例:

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

    兩個formBean:1.formBeanIndustry與表industry對應,2.formBeanEnterprise與表enterprise對應。

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

    Struts設置如下:

    ? <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 頁面上對 enterprise 表進行編輯:

    <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 的一個方法 ,用于插入或更新數(shù)據(jù)。 _frwd 參數(shù)是通用動態(tài) formBean net.newxy.struts_faces.DynaFormBean 的保留屬性 ,其值是 struts action 子屬性 <forward /> name 值。 _frwd=forward2 ,是要后臺 update 數(shù)據(jù)后轉到 jsp2.jsp 頁面。

    開發(fā)者應設置一名為 error 的全局轉發(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 操作時發(fā)生了錯誤,會轉到 error.jsp 頁面上來,并將錯誤信息顯示出來.

    ?

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

    在開發(fā)過程中,有時要在數(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()); // 錯誤信息以 message 為名保存到 request 中.

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

    }

    }

    }

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

    ?

    約定

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

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

    _dao DAO 類的別名;

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

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

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

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

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

    myUpdate是 DispatchAction子類的一個方法.

    ?

    newxy(新坐標)技術網(wǎng)站 http://www.newxy.net

    posted on 2006-07-14 09:05 newxy新坐標 閱讀(228) 評論(0)  編輯  收藏

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


    網(wǎng)站導航:
     
    主站蜘蛛池模板: 亚洲香蕉免费有线视频| 毛片免费观看网站| 亚洲三区在线观看无套内射| 精品国产日韩亚洲一区91| 免费无码又爽又刺激高潮的视频| 亚洲精品日韩专区silk| 国产精彩免费视频| 亚洲av永久无码精品三区在线4 | 免费中文字幕视频| 亚洲精品国产精品乱码不卞| 朝桐光亚洲专区在线中文字幕 | 亚洲午夜久久久久久久久久| 久久www免费人成看国产片| 国产亚洲精品不卡在线| 日本免费A级毛一片| 久久夜色精品国产噜噜亚洲AV| 69精品免费视频| 亚洲日韩精品无码专区加勒比| 日韩在线免费电影| 国产高潮流白浆喷水免费A片 | 亚洲福利中文字幕在线网址| 国产午夜无码片免费| 亚洲精品综合一二三区在线 | 国产精品免费视频一区| 国产久爱免费精品视频| 亚洲欧洲国产日韩精品| 青苹果乐园免费高清在线| 国产亚洲精品第一综合| 亚洲精品蜜桃久久久久久| 亚洲精品视频免费在线观看| 亚洲伊人久久大香线蕉AV| 亚洲 小说区 图片区 都市| 国产一区二区免费视频| 亚洲av日韩av无码av| 亚洲精品人成无码中文毛片 | 国产成人在线免费观看| 三年片在线观看免费西瓜视频 | 亚洲成a人片毛片在线| 午夜亚洲av永久无码精品| 少妇无码一区二区三区免费| 亚洲精品天堂成人片AV在线播放 |