定制一个灵活的struts框架

王朝java/jsp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

用过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中按自己的需要来写方法了,为将来单体的测试也很有用,毕竟四个参数全用的情况是很少的,当然,我现在写的还有问题,就是方法的名称不能重复,这是因为目前处理的地方不是很好,希望大家一起讨论........

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有  導航