现在有这样一个需求,action中需要这样实现这样一个功能:实体类information.javaprivate String id;
private String column1;
private String column2;
private String column3;
private String column4;
private String column5;
private String column6;
private String column7;
private String column8;
private String column9;Action类insert.java//...(其它代码略)
for (int i = 0; i < list.size(); i++) {
String str ="information.getColumn"+i+"();"; }问题来了,我需要将str这个字符串代码进行执行,且其中的变量i如代码所示是动态的,然后将get出的值放入一个字符串中。请大侠指教!反射javastring

解决方案 »

  1.   

    获得实际串值后解构出方法名,且又知道实例(实例为information,引用可以获得就直接用,不能获得的话,其类型也应该知道吧),不就可以通过反射调用了?
      

  2.   

    那就不要用BEAN了呗
    直接返回MAP就好了....
      

  3.   

    看下这个
    三、用反射机制调用对象的方法
    http://lavasoft.blog.51cto.com/62575/43218/
      

  4.   


    BeanInfo beanInfo=Introspector.getBeanInfo(Information.class,Object.class);//获得Information的bean描述
    PropertyDescriptor[] pds=beanInfo.getPropertyDescriptors();//获得Information的所有属性描述
    for(PropertyDescriptor pd:pds){
       String propName=pd.getName();//获得属性名
       if(propName.startsWith("column")){//以column开头的
          Method readMethod=pd.getReadMethod();//获得此属性的get方法
           Object value=readMethod.invoke(information);//在information对象向调用此get方法即获得值;value就是你要的
       }
    }