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