<form action="arrayaction.do" method="get">
<input type="button" value="插入"/>
<input type="button" value="删除"/>
<input type="button" value="修改"/>
<input type="button" value="显示"/>
</form>
问题是这样的:页面上有多个按钮,我需要当点击这些按钮时全部进入到同一个action中,但是这个action中有不同的方法.比如insert() delete()等,我点击不同按钮时怎么能够让它找到相应的方法呢?请详细说明,谢谢各位了!!

解决方案 »

  1.   

    你用的struts1吧,让你的action继承dispatchAction,然后在你的Action中写insert() delete()等,当提交的时候,把这个方法名传到后台就行了
      

  2.   

    你用的是struts什么版本,
    如果是struts2可以用下面的方法
    将Action类中的每一个处理方法都定义成一个逻辑Action方法。
    <!DOCTYPE struts PUBLIC
           "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
           "http://struts.apache.org/dtds/struts-2.0.dtd">
    <struts>
    <package name="my" extends="struts-default" namespace="/manage">
           <action name="userLogin" class="org.qiujy.web.struts2.action.LoginAction" method="login">
             <result name="success">/success.jsp</result>
             <result name="error">/error.jsp</result>
           </action>
          
           <action name="userRegist" class="org.qiujy.web.struts2.action.LoginAction" method="regist">
             <result name="success">/success.jsp</result>
             <result name="error">/error.jsp</result>
           </action>
    </package>
    </struts>如上,把LoginAction中的login和regist方法都配置成逻辑Action。要调用login方法,则相应的把index.jsp中表单元素的action设置为"manage/userLogin.action";要调用regist方法,把regist.jsp中表单元素的action设置为"manage/userRegist.action"。
    如果是struts1版本用下列方法
     继承action类继承LookUpDispatchAction
    第一步:创建一个项目一个jsp页面(MyJsp.jsp)
        <html:form action="login" method="post" >
            <bean:message key="label.username"/>
              <html:text property="userName" />
               <bean:message key="label.passWord"/>
              <html:password property="password" />
               <html:submit property="action">
    <bean:message key="button.add"/>
    </html:submit>
              <html:submit property="action">
    <bean:message key="button.update"/>
    </html:submit>
    <html:errors><!—用于显示验证时出现的错误-->
         </html:form>
    第二步:applicationResources.properties资源文件代码如下
    label.username=usename
    label.password=password
    button.add=add
    button.update=update
    error.required={0} is required
    error.username.length =Username must longer than 3
    error.password.length =Password must longer than 3
    第三步:创建一个ActionForm用于封装表单这里我以LoginForm命名代码如下:
    package superet.dept.form;
    import javax.servlet.http.HttpServletRequest;
    import org.apache.struts.action.ActionError;
    import org.apache.struts.action.ActionErrors;
    import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionMapping;
    import org.apache.struts.action.ActionMessage;
    public class LoginForm extends ActionForm {
    private String userName;
    private String passWord;
              public String getUserName() {
              return username;
    }
    public void setUserName(String username) {
              this.userName = userName;
    }
              /** 
               * Method validate用于验证表单
               * @param mapping
               * @param request
               * @return ActionErrors
               */
              public ActionErrors validate(
                        ActionMapping mapping,
                        HttpServletRequest request) {
                         ActionErrors errors = new ActionErrors();
                            if(this.username==null || this.username.length()<1){
                            ActionMessage error = new ActionMessage("error.required","User Name");
                              errors.add("user",error);
                            
                            }
                            if(this.username.length()<3){
                                errors.add("userLength",new ActionMessage("error.username.length"));
                              }
                            if(this. passWrod ==null || this. passWrod.length()<1){
                            ActionMessage error = new ActionMessage("error.required","User Name");
                              errors.add("passWrod ",error);
                            
                            }
                            if(this. passWrod.length()<3){
                                errors.add("userLength",new ActionMessage("error. passWrod.length"));
                              }
                            return errors;
              }
              /** 
               * Method reset
               * @param mapping
               * @param request
               */
              public void reset(ActionMapping mapping, HttpServletRequest request) {
                        // TODO Auto-generated method stub
              }
              public String getPassWord() {
                        return password;
              }
              public void setPassWord(String password) {
                        this.password = passWord;
              }
    }
    第四步:创建Action这里的Action继承了LookUpDispatchAction我以LoginAction命名.代码如下:
    package com.lyx.struts.action;
    import java.util.HashMap;
    import java.util.Map;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionForward;
    import org.apache.struts.action.ActionMapping;
    import org.apache.struts.actions.LookupDispatchAction;
    public class LoginAction extends LookupDispatchAction {
              public ActionForward add(
                                 ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response) {
    System.out.print("add");
                                 // TODO Auto-generated method stub
                                 return null;
                        }
              public ActionForward update(
                                 ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response) {
    System.out.println("update");
                                 // TODO Auto-generated method stub
                                 return null;
                        }
              protected Map getKeyMethodMap() {
                        System.out.print("hello map");
                        Map map=new HashMap();
                        map.put("button.add","add");
                        map.put("button.update"," update");
                        // TODO 自动生成方法存根
                        return map;
              }
    }
    注意:这里要把execute方法删掉.要不然不起作用.
       getKeyMethodMap()是一个抽象方法,必须得实现.这里用于映射.
    第五步:给出Strut-config.xml配置文件的信息.
    <?xml version="1.0" encoding="UTF-8"?>
    <struts-config>
    <data-sources />
    <form-beans >
    <form-bean name="loginForm" type="com.lyx.struts.form.LoginForm"/>
    </form-beans>
    <global-exceptions />
    <global-forwards />
    <action-mappings >
        <action
          input="/MyJsp.jsp"
          name="loginForm"
         parameter="action"
          path="/login"
          type="com.lyx.struts.action.LoginAction" />
    </action-mappings>
    <message-resources parameter="com.lyx.struts.ApplicationResources" />
    </struts-config>
    注意:这里Action里的Parameter属性的值是表单提交按钮属性的值.
    第六步:Ok了.测试一下吧
    点Add在控制台上输出Add
    点update在控制台上输出update 
      

  3.   

    1.通过js提交,不同的提交传方法变量到后台
     然后可以使用dispathaction
     或者程序判断js不熟的话 随便查下js提交
      

  4.   

       有些多楼主慢慢看,struts1一个action过个方法比较麻烦,不过解决方法就是上面那种,资源文件里写的是native转码后控件的value属性值,方法于按钮的对应是靠map映射来实现的。
      

  5.   

    action配置:
        <action path=""  type=""  
                 name="" 
                 scope="" 
                 parameter="action" 
                 input="">
        </action>jsp:
            <form action="arrayaction.do" method="get">
            <input type="button" value="插入" onclick="document.forms[0].action='arrayaction.do?action=insert'"/>
            <input type="button" value="删除" onclick="document.forms[0].action='arrayaction.do?action=delete'"//>
            <input type="button" value="修改" onclick="document.forms[0].action='arrayaction.do?action=modify'"//>
            <input type="button" value="显示" onclick="document.forms[0].action='arrayaction.do?action=display'"//>
      

  6.   

    在你的action配置里面加如parameter="action"这一段然后页面为<form action="arrayaction.do" method="get">
      <input type="button" value="插入" onClick="document.form[0].action='arrayaction.do?action=insert'"/>
      <input type="button" value="删除" onClick="document.form[0].action='arrayaction.do?action=delete'"/>
      <input type="button" value="修改" onClick="document.form[0].action='arrayaction.do?action=update'"/>
      <input type="button" value="显示" onClick="document.form[0].action='arrayaction.do?action=select'"/>
    </form>
      

  7.   

    用的是struts1实现的7楼和8楼的朋友的方法之前我想到过,不过我想问的就是那个继承dispathaction 的方法应该怎么做?
    能不能说详细些?
      

  8.   

    要是struts1用7楼的兄弟做的就不错了,是struts2的话用3楼兄弟做的吧
      

  9.   

    提交的路径加/shh/yfadddis.do?method=addDispensary配置文件上加parameter = "method"每次提交就会执行你method=的方法
      

  10.   

    upnew的action继承addDispensary   新建action的时候有这个选项,选一下就ok了。然后在action里面写上那四个方法。xml里面配置4四个action 每个action的parameter =“path” 
     path为 /shh/yfadddis.do?method=addDispensary 的addDispensary这样就ok啦。
      

  11.   

    struts1:配置文件里加个参数parameter="operation"  页面里提交的时候通过脚本,不同的按钮给operation的赋值不同 就可以区分了。在action里有个execute方法 在这个方法写上
    if (operation!= null && operation.equals("initquery")) {
    System.out.println("执行方法initquery")
        return initquery(mapping,form,request,response);
    }
    if (operation != null && operation.equals("query")) {
    System.out.println("执行方法query");

    return query(mapping,form,request,response);
    }
    这样就解决了。
    struts2就更简单了 通过js提交 直接在action上带上函数就可以了
      

  12.   

    继承LookupDispatchAction能解决这种问题。
      

  13.   

    给每个按钮name属性
    都提交到同一Action  获取不同的name 调不同方法
      

  14.   


    onclick="document.forms[0].action='arrayaction.do?action=insert'"form[0]后面的.action是什么意思?我感觉不应该.action呢
      

  15.   


    怎么在action中获得按钮的name?
      

  16.   

    document.forms[0].action=“path”是把表单要提交的路径赋值。改变表单提交的action相当于改了<form action="path" method="get"> 。
    怎么在action中获得按钮的name?
    这个就是在actio里面取form表单的值一样。
      

  17.   


    struts1里边可以用dispatchAction
      

  18.   


    具体说一下不就完了么?是用request对象获取么?
    给个表达式给我不就完了,我还不知道和获取form表单值一样!!!!有人知道么
      

  19.   

    谁能给我写个简单的dispatchAction的例子
      

  20.   

    package com.action;import java.util.List;import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionForward;
    import org.apache.struts.action.ActionMapping;
    import org.apache.struts.actions.DispatchAction;import com.formbean.ArticleForm;import utils.PageNumber;import bean.Article;
    import bean.articleTypeBean;import bean.articleBean;public class articleAction extends DispatchAction {
    public ActionForward findAll(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response) {
    // TODO Auto-generated method stub
    // List list = abean.findAll();
    // request.setAttribute("list", list);

    ArticleForm articleForm = (ArticleForm) form;// TODO Auto-generated method stub
    articleBean abean = new articleBean();
    int typeid=0;//类别编号
    int cpage = 1;//当前页

    if(request.getParameter("typeid")!=null)
    {
    typeid = Integer.parseInt(request.getParameter("typeid"));
    }
    if(request.getParameter("cpage")!=null)
    {
    cpage = Integer.parseInt(request.getParameter("cpage"));
    }
    List list = abean.findAll(typeid);

    PageNumber<Article> apage = new PageNumber<Article>();
    apage.setBigList(list);
    apage.setCurrentPage(cpage);
    request.setAttribute("apage", apage); // articleBean ab=new articleBean();
    // List list=ab.selectAll();
    // request.setAttribute("articlelist", list);
    request.setAttribute("url", "back_ArticleSelect.jsp");
    return mapping.findForward("suc");
    }
    public ActionForward insertArticle(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response) {
    // TODO Auto-generated method stub
            articleTypeBean atb=new articleTypeBean();
            List list=atb.selectTypeName();
            
            request.setAttribute("type", list);

    request.setAttribute("url", "back_ArticleAdd.jsp");
    return mapping.findForward("suc");
    }
    public ActionForward saveArticle(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response) {
    // TODO Auto-generated method stub
           ArticleForm af=(ArticleForm)form;
           articleBean ab=new articleBean();
           int count= ab.saveArticle(af);
           if(count>0){
            request.setAttribute("url", "right.jsp");
           }else{
            request.setAttribute("url", "right.jsp");
           }
           
           


    return mapping.findForward("suc");
    }

    }
    很久以前写的东西了  可能不是很好 个位别笑话啊
      

  21.   

    同一个Action多个方法,继承DispatchAction 
    用js控制不用的按钮提交到不用的url(insertemp.do/pudateemp.do )public class EmpAction extends MappingDispatchAction { public ActionForward insert(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response)
    throws Exception {
    //.......
    return mapping.findForward("success");
    } public ActionForward update(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response)
    throws Exception {
            //......
    return mapping.findForward("success");
    }}struts-config.xml的配置
            <action-mappings>
    <action path="/insertemp" type="action.EmpAction"
    parameter="insert">
    </action> <action path="/updateemp" type="action.EmpAction"
    parameter="update">
    </action>
    </action-mappings>
      

  22.   

    配置多个action,对应你的那个类的不同方法。
    然后让页面分别提交到不同的action。
      

  23.   

    页面上要用js提交到不同的action时,
    form.action="/admin/myaction.do?methodp=findNews";
    form.submit();后台要配置 <action path="/admin/myaction"  name="newsForm"  parameter="methodp"  scope="request" type="org.springframework.web.struts.DelegatingActionProxy">
         <forward name="findNews" path="/admin/news/news_admin.jsp"   />
         <forward name="editNews" path="/admin/news/edit_news.jsp"   />
      </action>
      

  24.   


    action标签中的parameter属性是什么意思?
      

  25.   

    action配置一个parameter值随便我用的是OP
    <action-mappings>
    <action validate="false" type="com.accp.action.UserAction" path="/user"
    parameter="op">
    </action>
    继承DispatchAction 不用实现execute定义多个业务控制方法
    public class XxxxAction extends DispatchAction {
    public ActionForward Add(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response) 
    {
    return null;
    }
    hmtl中
    在链接至Action时 url中传递一个op参数【为刚才Action配置的parameter】 值为action中的方法名在链接至改Action会自动找到该方法执行如:
    <html:link href="/xxxAction?op=Add" >添加</html:link>  <html:link href="/xxxAction?op=Select" >查询</html:link>
      

  26.   

    action标签中的parameter属性是什么意思?parameter为链接到你的Action是传递一个参数给Action这个参数名字就是parameter="xx"的这个xx
    这样当
    url="http://localhost:8080/test/TestAction?xx=Add"
    这个xx参数传递的值Add就是我们TestAction中定义的方法
     
      

  27.   

    想在一个action中处理多个方法可以继承DispatchAction,然后在配置文件action标签中设置parament,这个parament接受页面传来的参数的值,然后回去匹配你的action中方法
    举例
    客户端的请求路径为myAction.do?method=fun1,而你的parament的值就是method,他会接受客户端method的值,这里是fun1,然后它回去找你的action里面有没有fun1方法
      

  28.   

       楼主可以定义一个参数parameter=method 然后在请求路径上加上参数即可 
      

  29.   

    不用JavaScript、DispatchAction就可以来做:
    <form action="arrayaction.do" method="get">
            <input type="button" value="插入" name="insert"/>
            <input type="button" value="删除" name="delete"/>
            <input type="button" value="修改" name="modify"/>
            <input type="button" value="显示" name="list"/>
    </form>
    假如用户点了“插入”按钮,这时: 
    request.getParameter("insert")=="插入";
    request.getParameter("delete")==null;
    request.getParameter("modify")==null;
    request.getParameter("list")==null;以此类推!也就是说无论多少submit类型的按钮,只有一个不为空。这时在Struts1的Action中以用反射方法来做:
    public class MyAction extends Action{//注意,这里并不需要继承DispatchAction    public ActionForward execute(ActionMapping mapping, ActionForm from, HttpServletRequest request, HttpServletResponse response) throws Exception {
            String func = {"insert","delete","modify","list"};
            String methodName = null;
            for(String f : func){
                    if(request.getParameter(f) != null){
                        methodName = request.getParameter(f);
                        break;
                    }
            }
            try {
                //利用反射技术让程序自动找到要处理的方法并执行之,注意methodName参数
                Method method = this.clazz.getDeclaredMethod(methodName, ActionMapping.class, ActionForm.class, HttpServletRequest.class, HttpServletResponse.class);
                return (ActionForward)method.invoke(this, mapping, from, request, response);
            } catch (Exception e) {
                request.setAttribute("message", e.getMessage());
                return mapping.findForward("message");
            }
        }    public ActionForward insert(ActionMapping mapping, ActionForm from, HttpServletRequest request, HttpServletResponse response) throws Exception {
            //todo
        }    public ActionForward delete(ActionMapping mapping, ActionForm from, HttpServletRequest request, HttpServletResponse response) throws Exception {
            //todo
        }    public ActionForward modify(ActionMapping mapping, ActionForm from, HttpServletRequest request, HttpServletResponse response) throws Exception {
            //todo
        }    public ActionForward list(ActionMapping mapping, ActionForm from, HttpServletRequest request, HttpServletResponse response) throws Exception {
            //todo
        }}
      

  30.   

    照2楼说的做就行了,就只要继承dispatchAction类,你的配置是可以了
      

  31.   

    1,用js如7楼的
    1,用struts自己的LookupDispatchAction 或dispatchAction两种皆可。。
      

  32.   

    LZ好好看看7楼8楼11楼29楼的,你在struts-config.xml里面配置action的时候指定parameter="op",然后提交的时候就会根据op=“...”去找对应的方法。op="addValue"它就去找addValue方法。
      

  33.   

    我用了dispatchaction  可我为什么边表单上的数据都不能获取了呢??
      

  34.   


    <form action="arrayaction.do" method="get">
      <input type="button" value="插入" onClick="document.form[0].action='arrayaction.do?action=insert'"/>
      <input type="button" value="删除" onClick="document.form[0].action='arrayaction.do?action=delete'"/>
      <input type="button" value="修改" onClick="document.form[0].action='arrayaction.do?action=update'"/>
      <input type="button" value="显示" onClick="document.form[0].action='arrayaction.do?action=select'"/>
    </form>8楼的就可以了
      

  35.   

    用struts2实现就相当方便
    当然struts1实现也很容易