<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類+通用持久類+通用動態formBean類+通用DispatchAction類,實現數據增、刪、改、查

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

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

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

    通用動態 formBean 類: net.newxy.struts_faces.DynaFormBean

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

    在《 DAO + 通用持久類 + 通用動態 formBean 類,實現數據增、刪、改、查》(又名《web開發:通用持久類代替hibernate的持久類、通用動態formBean類代替struts的formBean類》)文章中已介紹了 DAO 類、通用持久類、通用動態 formBean 類在數據增、刪、改、 查中的運用。本篇增加了一個類net.newxy.struts_faces.DispatchAction,目的是為開發者提供幾個通用的DispatchAction方法,節省代碼,增加開發效率。

    一、?????? 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 參數值作為 action 子屬性 <forward /> name 值.如果此值不空,以其為 forward name 值轉發,如果沒有此參數或其值為空, actionMapping 會查看 action input 參數,如果有 input forward ,則轉到 input forward ,否則將"沒有轉發路徑"類的信息保存到 request 中,并以名為 ”error” 的全局 forward 進行轉發.

    2 find

    jsp 頁面表單傳來的數據在這里組合成 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的父類

    如果發生異常,將異常信息以“ message” 為名保存到 request 中,并以“ error” 為全局 forward name 值進行轉發:

    ?????? 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>

    如果數據查詢過程沒有異常,執行 return getActionForward(actionMapping,httpServletRequest) 語句轉發。

    ?

    3 update

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

    異常處理與轉發和find方法相同。

    ?

    4、remove

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

    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。

    異常處理與轉發和find方法相同。

    ?

    5、edit

    edit方法從httpServletRequest.getParameter("_index")得到記錄索引號,找到這條記錄,將其數據填到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的父類。

    異常處理與轉發和find方法相同。

    ?

    6、empty

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

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

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

    異常處理與轉發和find方法相同。

    ?

    二、建議和約定

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

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

    舉例:

    兩個表:1.行業表industry,2.企業表enterprise。

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

    兩個DispatchAction: 1.actionIndustry與formBeanIndustry關聯,有兩個轉發條件,2.actionEnterprise與formBeanEnterprise關聯,也有兩個轉發條件。

    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>

    現在在 jsp1.jsp 頁面上對 enterprise 表進行編輯:

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

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

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

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

    ? </html:submit>

    </html:form>

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

    開發者應設置一名為 error 的全局轉發:

    ? <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 在執行 update 操作時發生了錯誤,會轉到 error.jsp 頁面上來,并將錯誤信息顯示出來.

    ?

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

    在開發過程中,有時要在數據插入或更新到數據庫前從formBean中提取數據,或更改formBean中的數據.建議開發者的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{

    ……? //開發者代碼

    try{

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

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

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

    catch(Exception e){

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

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

    }

    }

    }

    數據上傳到后臺,開發在將數據送到數據庫前,改變了field1字段屬性值.

    ?

    約定

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

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

    _dao DAO 類的別名;

    _table :要插入 更新或刪除數據的表名;

    _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(新坐標)技術網站 http://www.newxy.net

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

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


    網站導航:
     
    主站蜘蛛池模板: 中文字幕亚洲男人的天堂网络| 九月婷婷亚洲综合在线| 亚洲高清中文字幕免费| 国产大片免费网站不卡美女 | 亚洲av鲁丝一区二区三区| 久久精品国产精品亚洲人人| 亚洲乱码日产精品a级毛片久久| 免费成人黄色大片| 亚洲一级片免费看| 毛茸茸bbw亚洲人| 久久亚洲高清观看| 久久精品国产亚洲av影院| 亚洲精品视频久久| 国产精品亚洲综合五月天| 亚洲狠狠婷婷综合久久| 国产成人亚洲毛片| 久久久久久av无码免费看大片| a级毛片在线免费观看| 久久久免费精品re6| 美女被免费喷白浆视频| 日日操夜夜操免费视频| 亚洲精品成人区在线观看| 人人狠狠综合久久亚洲婷婷| 在线电影你懂的亚洲| 在线观看亚洲AV每日更新无码| 亚洲AV无码专区在线厂| 成年大片免费高清在线看黄| 中国一级特黄高清免费的大片中国一级黄色片 | 毛片免费vip会员在线看| 四虎影视在线永久免费看黄| 久久精品亚洲福利| 久久精品国产亚洲AV无码麻豆| 激情综合亚洲色婷婷五月APP | 亚洲av之男人的天堂网站| 亚洲熟妇无码久久精品| 久久亚洲精品无码网站| 久久er国产精品免费观看8| 69免费视频大片| 国产成人高清精品免费鸭子| 亚洲免费观看视频| 国产成人精品日本亚洲专区6|