建议使用DispatchAction

随着项目的发展,struts-config.xml配置文件、action、form等等都变的庞大起来,如果我们继续使用普通action,那我们就显得不明智了。建议使用DispatchAction,把相关操作集中到一个Action中,我们不用再去重新定义execute()方法。例如可以在DispatchAction中增加如下方法:

  • public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
  • public ActionForward insert(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
  • public ActionForward update(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception

同时在struts-config.xml中定义类似的内容:

<action path="/saveSubscription" type="org.apache.struts.actions.DispatchAction" name="subscriptionForm" scope="request" input="/subscription.jsp" parameter="method"/>

其实关键是增加parameter属性。

调用方法如下:

http://localhost:8080/myapp/saveSubscription.do?method=update

记得要使用method=xx,xx代表被调用DispatchAction中的方法名

备注:建议大家去看看struts api,那里有详细的说明。

使用DispatchAction跳转页面问题?????

10-21

最近做了一个Struts+Hibernate(增、删、改、查)的练习,功能是都实现了,但在删除的时候,我想提交后再返回到原来的页面,在DispatchAction的删除方法里和配置文件里面试过都不成功,请各位高手指点一下,小弟万分感谢!rn也就说我想在返回到练习中的selectStu.jsp这个页面rn以下是我写的代码:rnrn[color=#008000]====Action代码:====[/color]rnpackage com.aptech.jb.action;rnrnimport java.util.List;rn......rnrnpublic class HibTestAction extends DispatchAction rnrn stuentBiz stubiz = new studentBizImpl();rnrn // 插入学生记录rn public ActionForward doAdd(ActionMapping mapping,rn ActionForm form,rn HttpServletRequest request,rn HttpServletResponse response) rn HibTestForm htf = (HibTestForm) form;rn try rnrn stubiz.add(htf);rnrn return mapping.findForward("succeed");rn catch (Exception e) rn return mapping.findForward("error");rn rn rnrn // 查询学生记录rn public ActionForward doSearch(ActionMapping mapping,rn ActionForm form,rn HttpServletRequest request,rn HttpServletResponse response) rn //HibTestForm htf = (HibTestForm) form;rn //查询学生记录rn //String stuId=request.getParameter("stuId");rn //int stuid=Integer.parseInt(stuId);rn rn tryrn List list=stubiz.allStu();rn request.setAttribute("list", list);rn return mapping.findForward("toSearch"); rn catch(Exception e)rn return mapping.findForward("error");rn rn rnrn //更新学生信息rn public ActionForward doUpdate(ActionMapping mapping, ActionForm form,rn HttpServletRequest request, HttpServletResponse response) rn rn HibTestForm htf= (HibTestForm) form;rn ActionForward af=null;rn // TODO Auto-generated method stubrn tryrn stubiz.update(htf);rn af=mapping.findForward("succeed");rn catch(Exception ex)rn af=mapping.findForward("error");rn ex.printStackTrace();rn rn return af;rn rnrn //删除学生信息rn public ActionForward doDelete(ActionMapping mapping, ActionForm form,rn HttpServletRequest request, HttpServletResponse response) rn //HibTestForm htf = (HibTestForm) form;rn //删除学生记录rn ActionForward af=null;rn rn String stuId =request.getParameter("STUID");rn int stuid=Integer.parseInt(stuId);rn stubiz.del(stuid);rn [color=#FF0000]af=mapping.findForward("toSearch1");rn return af;[/color]rn rnrnrnrn[color=#008000]====配置文件代码:====[/color]rn rn rn rn rn rn [color=#FF0000][/color]rn rn rnrn rnrn rn[color=#008000]====selectStu.jsp代码====[/color]rn<%@ page language="java" contentType="text/html; charset=gbk" %>rn<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>rn<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>rn<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>rnrn rn 学生信息rn rn rn rn rn rn rn rn rn rn rn 学生编号rn rn 姓名rn rn 性别rn rn 出生年月rn rn 家庭住址rn rn rn 操 作rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn 更新rn [color=#FF0000] 删除rn [/color] rn rn rn rn rn rnrnrn

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试

关闭