1.前言
最近在做一個javaweb項目練練手,涉及到把jsp頁面中表單的內容存到數據庫,和request.getParameterMap
配合使用可以將jsp頁面表單的數據轉化為bean對象。
2.介紹
BeanUtils位於org.apache.commons.beanutils.BeanUtils下面,其方法populate的作用解釋如下:
完整方法:
BeanUtils.populate( Object bean, Map properties ),
這個方法會遍歷map<key, value>中的key,如果bean中有這個屬性,就把這個key對應的value值賦給bean的屬性。
3.使用
在獲取jsp頁面中表單數據的時候,剛好可以使用request.getParameterMap
得到表單數據的map集合
Map<String, String[]> map = request.getParameterMap();
配合BeanUtils.populate
使用,將map集合轉化為bean對象
BeanUtils.populate(user,map);