随笔-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新坐标 阅读(218) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: