简体   繁体   中英

How to set atributes throught reflexion

I have map of <String, Object>:

params={
  dateOfBirthTo=23.05.2013,
  lastName=bbb, ssn=aa-ccc-ddd,
  gender=MALE,
  dateOfBirthFrom=03.05.2013,
  firstName=aaa
}

Then I have form which contains variable from this map. How I can create new form with this value through reflection?

Something like:

SimpleForm form = new SimpleForm();

Map<String, Object> parameters = request.getParams();
for (Map.Entry<String, Object> entry : parameters.entrySet()) {
  // fill form
}

You could use Apache Commons BeanUtils

SimpleForm form = new SimpleForm();

Map<String, Object> parameters = request.getParams();
for (Map.Entry<String, Object> entry : parameters.entrySet()) {
    BeanUtils.setProperty(form, entry.getKey(), entry.getValue());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM