我按照网上说的 首先是类继承DispatchAction ,然后写了些测试代码 代码如下:public class LoginAction extends DispatchAction { //继承了DispatchAction public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {

request.getSession().setAttribute("operation", "execute");
return mapping.findForward("error.jsp");
}
//login方法
public ActionForward login(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {

request.getSession().setAttribute("operation", "login");
return mapping.findForward("error.jsp");
}
//register方法
public ActionForward register(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {

request.getSession().setAttribute("operation", "register");
return mapping.findForward("error.jsp");
}
}
然后struts配置文件中parameter="operation"<action
    path="/login"
    type="com.mulan.struts.action.LoginAction"
    scope="request"
    input="/index.jsp"
    validate="false"
    parameter="operation" >
</action>
然后index.jsp文件<form action="login.do?operation='login'" method="post">
   用户名:<input type="text" name="temail"><br>
   密码:<input type="password" name="tpass"><br>
   <input type="submit" value="登录">&nbsp;&nbsp;
   <a href="register.jsp">加入我们</a>
</form>
照理来说 提交以后 应该调用login方法,可是好像没反应.
地址栏上的地址变成了 [path]/login.do?operation='login'

解决方案 »

  1.   

    DispatchAction继承自Action类,它是一个抽象类,封装了一些基础方法,来解决使用一个Action处理多个操作的能力,这就是DispatchAction最大的用途,它可以帮助我们用一个Action类,封装一套类似的操作方法,节省了类的数目,同时也减轻了后期维护的困难。struts1.x里面的
      

  2.   

    fangmingshijie 大侠 
    问题是应该怎么使用它呢 我的代码有没有什么问题 ? 
      

  3.   

    <form action="login.do?operation=login" method="post">去掉引号
      

  4.   

    <form action="login!login" method="post">应该是这样。
      

  5.   

    struts1应该是这么写的。 根据parameter调用属性的值找到方法调用  
    你把你那session里面的值那名字换下试试,我经常出现重名导致一些问题~ 总之多试下
      

  6.   

    哦 对了。 继承这个类好像是不能重写execute方法的。 struts1很久没用了。 你把这集中情况都试试
      

  7.   

    a9819zj1  都不行啊 都没有重名了 如果不重写execute 就会抛异常啊 
      

  8.   

    我查了下是不需要重写execute方法的。异常是什么
      

  9.   

    execute  方法中 接受  operation的值,  然后判断是login了 传入 login判断中
      

  10.   

    这是dispatchaction的execute方法。 我刚看了下。 dispatchaction应该就是通过她本书execute方法来实现一个类实现多个功能。    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
            if (isCancelled(request)) {
                ActionForward af = cancelled(mapping, form, request, response);            if (af != null) {
                    return af;
                }
            }        // Get the parameter. This could be overridden in subclasses.
            String parameter = getParameter(mapping, form, request, response);        // Get the method's name. This could be overridden in subclasses.
            String name =
                getMethodName(mapping, form, request, response, parameter);        // Prevent recursive calls
            if ("execute".equals(name) || "perform".equals(name)) {
                String message =
                    messages.getMessage("dispatch.recursive", mapping.getPath());            log.error(message);
                throw new ServletException(message);
            }        // Invoke the named method, and return the result
            return dispatchMethod(mapping, form, request, response, name);
        }
    还有6楼写的那个是struts2里面的。 那个!可以直接调用action里的方法
      

  11.   

    a9819zj1 
    恩恩 谢谢 按照你的思路 把问题解决了 .. 
    其实 .. 问题有两个 
    一个是因为我页面跳转出错了 
    另外一个 不知道是不是因为我重写了 execute 方法 struts总是跑到 execute里面去 总之谢谢大家了 结贴了