我有一堆方法只想通过1个action来访问,通过不同的method来达到访问不同的方法。比如访问一个地址test.actiontest.action?method=list.users
test.action?method=get.user
test.action?method=remove.users
test.action?method=save.users从而进入list.users方法
get.user方法
remove.users方法
save.users方法
我根据不同的method参数进入不同的方法怎么能做到?难道就只有动态获取代理来invoke执行吗?还有没有其他的一些方法。先谢谢了。action

解决方案 »

  1.   

    如果只是你题目说的。
    那么有很多方法,不用框架。
    用servlet的时候,不是根据页面打个标记然后调用哪个方法的吗?
      

  2.   

    有没有引用资料或者例子的链接 ,我去看看。其实想知道taobao是对这方面如何处理的。
    因为提交的action全都是一个 通过不同的method来处理不同的方法。
    http://api.taobao.com/apitools/apiTools.htm
      

  3.   

    1 反射2 if else 穷举还想要什么方法?
      

  4.   

    不是玩玩  
    taobao api 确实是一个路径访问通过method访问不同的方法
    想制作一个对外公开的api 通过访问一个action 带上不同的method来进行访问不同的方法
      

  5.   


    2的话可能就很勉强了。毕竟是对外公开的api。
    方法以后多的话得穷举死我啊
      

  6.   

    function(arr){
        var url = "test.action?method="+arr
    }这样不行吗?
      

  7.   


    还是不了解你的意思。感觉你的意思是action中的方法是动态的。是不是?
    这样就符合你说的那意思了。如果这样的话,也没什么吧。这时候就需要个协议呗。
    我没看过淘宝那个。
    不过我感觉需要个协议的。
    每个自己命名的方法自己记住。或者根据ID来的。讨论到这里貌似还是没看出有什么意义如果你的服务端的action中的方法是定死的。。或者不是动态的。。
    那么就该怎么访问就怎么访问呗别人调用就让他写好他的后缀不就OK了?
      

  8.   

    无论反射还是if else 都要后台实现了对应的方法才能真正起作用吧。
      

  9.   

    自己算是解决了。struts2框架有方法解决使用
    action!method.action
    红线部分是不变的test.action?method=listUsers
    test.action?method=listUsers
    test.action?method=listUsers
    xxxxx/test!listUsers.action
    xxxxx/test!getUser.action
    xxxxx/test!listUsers.action
    xxxxx/test!listUsers.action对于我的需求就可以直接这样写查了查资料
      

  10.   

    敲了ctrl + enter了。。提交了。对上面重写下。。自己算是解决了。
    查了查资料
    struts2框架有方法解决使用
    action!method.action
    红线部分是不变的
    action是你的action
    method是你想进入的方法名test.action?method=listUsers
    test.action?method=getUsers
    test.action?method=removeUsersxxxxx/test!listUsers.action
    xxxxx/test!getUser.action
    xxxxx/test!removeUsers.action
    对于我的需求就可以直接这样写先写一个固定地址的action 例 trans.action  里面接受method参数在返回的时候  redirect 到 test!${method}.action这样就可以实现了。最后谢谢各位的回答和见解。贴下具体实现
    TransAction.java
    package com.test.action;
    import com.opensymphony.xwork2.ActionSupport;
    public class TransAction extends ActionSupport{    private String method ;
        
        @Override
        public String execute() throws Exception {
            return SUCCESS;    
        }
        
        public String method1()throws Exception{
         System.out.println("method1");
         return SUCCESS;
        }
        
        public String method2()throws Exception{
         System.out.println("method2");
         return SUCCESS;
        }
        
        public String method3()throws Exception{
         System.out.println("method3");
         return SUCCESS;
        } public String getMethod() {
    return method;
    } public void setMethod(String method) {
    this.method = method;
    }
        
        
        
    }
    struts2.xml<package name="test-teste" extends="struts-global" namespace="/test">
        
    <action name="trans" class="com.action.TransAction" method="execute">
         <result name="success" type="redirect">test!${method}.action</result>
    </action>
        
         <action name="test" class="cn.boyu.projects.bojin.transaction.action.TransationAction">
         </action>
    </package>
    访问路径:
    http://localhost/test/trans.action?method=method1
    http://localhost/test/trans.action?method=method2
    http://localhost/test/trans.action?method=method3