org.apache.commons.beanutils.BeanUtils使用示例

顾名思义,这是apache的提供的,方便我们对JavaBean进行操作。


注意点:使用setProperty的时候,参数名称 需要和javabean的属性名称保持一致,数值类型要一致


步骤一:

导包:导入commons-beanutils-1.8.3 

与 commons-logging-1.1.3 

步骤二:

示例代码:

CustChannelAttach info = new CustChannelAttach();

Enumeration names = request.getParameterNames();
if(names!=null){
while (names.hasMoreElements()) {
String name = (String) names.nextElement();
String value = request.getParameter(name);
System.out.println(name + "@@@@@" + value);
try {
BeanUtils.setProperty(info, name, value);
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}


示例代码变种(使用放射):

Class class1 = Class.forName("com.mw.mbox.common.channel.attach.model.CustChannelAttach");
Enumeration names = request.getParameterNames();
if(names!=null){
while (names.hasMoreElements()) {
String name = (String) names.nextElement();
String value = request.getParameter(name);
System.out.println(name + "@@@@@" + value);
try {
BeanUtils.setProperty(class1.newInstance(), name, value);
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}



你可能感兴趣的:(Java)