日期:2014-05-18  浏览次数:20670 次

写了个让EL可以call任何方法的工具
package   bib.action;

import   java.lang.reflect.InvocationTargetException;
import   java.lang.reflect.Method;

public   class   MethodCallUtil   {
Object   o   =   null;
String   method   =   null;
Object[]   args   =   null;

public   void   setMethod(String   method){
this.method   =   method;
}

public   void   setCallee(Object   o){
this.o   =   o;
}

public   void   setArgs(Object[]   args){
this.args   =   args;
}

public   Object   getCall(){
Object   ret   =   null;
Class[]   params   =   null;
if(this.args   !=   null){
params   =   new   Class[this.args.length];
for(int   i   =   0;   i   <   this.args.length   ;   i++){
params[i]   =   this.args[i].getClass();
}
}
try   {
Method   method   =o.getClass().getMethod(this.method,   params);
ret   =   method.invoke(o,   args);
}   catch   (SecurityException   e)   {
//   TODO   Auto-generated   catch   block
e.printStackTrace();
}   catch   (NoSuchMethodException   e)   {
//   TODO   Auto-generated   catch   block
e.printStackTrace();
}   catch   (IllegalArgumentException   e)   {
//   TODO   Auto-generated   catch   block
e.printStackTrace();
}   catch   (IllegalAccessException   e)   {
//   TODO   Auto-generated   catch   block
e.printStackTrace();
}   catch   (InvocationTargetException   e)   {
//   TODO   Auto-generated   catch   block
e.printStackTrace();
}

return   ret;  
}
}


EL   只能用来使用JAVA   BEAN,   有时挺不方便,   今天想CALL一个COLLECTION的SIZE方法,不行。   就写了这么个小工具。

------解决方案--------------------
楼主,写的真好!
------解决方案--------------------
mark

太棒了!
------解决方案--------------------
有求size的方法
${fn:length(products)}
------解决方案--------------------
good