使用BeanUtils.populate將map集合封裝為bean對象


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);





免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM