龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 软件开发 > JAVA开发 >

定制一个灵活的struts框架[图]

时间:2009-12-23 15:42来源:未知 作者:admin 点击:
分享到:
用过struts的朋友应该都知道,struts的action类提供的execute方法有四个参数,这是一种非常大范围的解决方案,而现实中我们的Action类中的方法可能只处理或者只需要个别参数,也许只需要acti

用过struts的朋友应该都知道,struts的action类提供的execute方法有四个参数,这是一种非常大范围的解决方案,而现实中我们的Action类中的方法可能只处理或者只需要个别参数,也许只需要actionform,或者甚至你都不需要,下面的代码为大家展示如何通过Java的反射机制来灵活实现.
  -------------
        /*  all code should insert into excute method of actionclass*/
       
        /*  get the type of the current actionclass*/
        Class clazz = this.getClass(  );
       
       
        /*      get all the methods in this actionclass       */
        Method[] methods = clazz.getMethods;
       
        /*      put all the method into hashmap               */
        HashMap methodss = new HashMap(  );
        for(int i=0;i<methods.length;i++){
        methodss.add(methods[i].getname(),methods[i])
        }
       
        /*      get the name of the method you want invokde  */
        String methodsname=request.getparameter("methodsname");
       
       
        /*      choose the method that you want invoke      */
        Class[] para= methodss.get("methodsname").getParameterTypes();
        try {
    if (para.length==1){
       if (para[0].getClass().newInstance() instanceof  ActionForm) {
        
      Object[] argst ={form};
      return (ActionForward) method.invoke( this,  argst);
      
     }
      .................................. 
       
      }
   } catch (InstantiationException e) {
     TODO Auto-generated catch block
    e.printStackTrace();
   }
  
  
  -----------
  
  这样,开发人员就可以在action中按自己的需要来写方法了,为将来单体的测试也很有用,究竟四个参数全用的情况是很少的,当然,我现在写的还有问题,就是方法的名称不能重复,这是因为目前处理的地方不是很好,希望大家一起讨论........


  
精彩图集

赞助商链接