E***@nokia.com
2002-10-08 07:46:22 UTC
Hi,
If I have this bean:
public class TestBean extends Object {
private String []roles = null;
private TestBean nested = null;
public TestBean() {
}
public String[] getRoles() {
return roles;
}
public void setRoles(String[] roles) {
this.roles = roles;
}
public String getRoles(int i) {
return roles[i];
}
public void setRoles(int i, String s) {
roles[i] = s;
}
public TestBean getNested() {
return nested;
}
public void setNested(TestBean nested) {
this.nested = nested;
}
}
And I run this code:
public static void main(String args[]) throws Exception {
TestBean testBean = new TestBean();
testBean.setNested(new TestBean());
Map values = new HashMap();
final String []roles = {"role1","role2"};
values.put("roles",roles);
values.put("nested.roles",roles);
BeanUtils.copyProperties(testBean,values);
System.out.println(testBean.getRoles().length);
System.out.println(testBean.getNested().getRoles().length);
}
I get this exception:
java.lang.IllegalArgumentException: Nested property names are not allowed
at org.apache.commons.beanutils.PropertyUtils.setSimpleProperty(PropertyUtils.java:1612)
at org.apache.commons.beanutils.BeanUtils.copyProperty(BeanUtils.java:363)
at org.apache.commons.beanutils.BeanUtils.copyProperties(BeanUtils.java:241)
at TestBeanUtils.main(TestBeanUtils.java:16)
Can somebody please tell me how to copy from a Map to nested bean properties?
Many thanks,
Mike.
If I have this bean:
public class TestBean extends Object {
private String []roles = null;
private TestBean nested = null;
public TestBean() {
}
public String[] getRoles() {
return roles;
}
public void setRoles(String[] roles) {
this.roles = roles;
}
public String getRoles(int i) {
return roles[i];
}
public void setRoles(int i, String s) {
roles[i] = s;
}
public TestBean getNested() {
return nested;
}
public void setNested(TestBean nested) {
this.nested = nested;
}
}
And I run this code:
public static void main(String args[]) throws Exception {
TestBean testBean = new TestBean();
testBean.setNested(new TestBean());
Map values = new HashMap();
final String []roles = {"role1","role2"};
values.put("roles",roles);
values.put("nested.roles",roles);
BeanUtils.copyProperties(testBean,values);
System.out.println(testBean.getRoles().length);
System.out.println(testBean.getNested().getRoles().length);
}
I get this exception:
java.lang.IllegalArgumentException: Nested property names are not allowed
at org.apache.commons.beanutils.PropertyUtils.setSimpleProperty(PropertyUtils.java:1612)
at org.apache.commons.beanutils.BeanUtils.copyProperty(BeanUtils.java:363)
at org.apache.commons.beanutils.BeanUtils.copyProperties(BeanUtils.java:241)
at TestBeanUtils.main(TestBeanUtils.java:16)
Can somebody please tell me how to copy from a Map to nested bean properties?
Many thanks,
Mike.