现在有这样一个需求,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
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
解决方案 »
- 求会使用Spring Tool Suite的前辈。
- 学了servlet和jsp后,想入学j2ee,不知道大家有什么推荐的书没?
- java写文件在windows下可以,部署到linux下后不行。
- java中替换特殊字符问题
- 干软件做外包有提升吗
- lomboz-all-in-one-R-3.3在下面Window-->Preference下不能看到Lomboz
- SQL问题
- 不允许的操作: streams type cannot be used in batching
- 生成excel out of memory 问题
- 求助:关于JAVA鼠标提取注释的问题
- 帮忙推荐下产品加密的技术
- 如何实现快递查询接口
直接返回MAP就好了....
三、用反射机制调用对象的方法
http://lavasoft.blog.51cto.com/62575/43218/
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就是你要的
}
}