2012년 4월 29일 일요일

apache BeanUtils.populate에서 user_name 형식의 속성을 userName 멤버변수에 입력하도록 하기


public static void populate(Object bean, Map properties) throws IllegalAccessException, InvocationTargetException {
// Do nothing unless both arguments have been specified
if ((bean == null) || (properties == null)) {
return;
}

// Loop through the property name/value pairs to be set
Iterator entries = properties.entrySet().iterator();
while (entries.hasNext()) {

// Identify the property name and value(s) to be assigned
Map.Entry entry = (Map.Entry) entries.next();
String name = (String) entry.getKey();
if (name == null) {
continue;
}
char[] del = {'_'};
name = StringUtils.replace(WordUtils.capitalize(name, del), "_", "");
if (name.length() > 1) {
name = name.substring(0,1).toLowerCase() + name.substring(1);
}

// Perform the assignment for this property
BeanUtils.setProperty(bean, name, entry.getValue());
}
}