顾名思义,这是apache的提供的,方便我们对JavaBean进行操作。
注意点:使用setProperty的时候,参数名称 需要和javabean的属性名称保持一致,数值类型要一致
步骤一:
导包:导入commons-beanutils-1.8.3 包
与 commons-logging-1.1.3 包
步骤二:
CustChannelAttach info = new CustChannelAttach();
Enumeration<String> 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<String> 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();
}
}
}
本文介绍如何使用Apache Commons BeanUtils库简化JavaBean属性设置过程。通过示例代码展示如何从请求中获取参数并设置到JavaBean对象中,同时提供了一种使用反射机制实现的方法。
8491

被折叠的 条评论
为什么被折叠?



