BeanUtils.populate的作用

转载自:http://blog.sina.com.cn/s/blog_84f5d20b0100tyuw.html

 很容易理解的一篇文章

 

首先,它是在org.apache.commons.beanutils.BeanUtils包中的一个方法。

方法的作用:用来将一些 key-value 的值(例如 hashmap)映射到 bean 中的属性。
 
servlet中有这样的使用:
先定义form表单内容的Info对象(当然你要先写一个bean,这个bean中包含form表单中各个对象的属性)
    InsuranceInfo info = new InsuranceInfo();  (这是一个javabean)
    BeanUtilities.populateBean(info, request);
——> populateBean(info, request.getParameterMap());(先将request内容转为Map类型)
——>BeanUtils.populate(info, propertyMap);(调用包中方法映射)
 
映射的过程就是将页面中的内容先用request获得,然后再将之转换为Map(这里用request.getParameterMap())
最后使用BeanUtils.populate(info,map)方法将页面各个属性映射到bean中。之后我们就可以这样使用bean.getXxxx()来取值了。

猜你喜欢

转载自blog.csdn.net/qq_33146717/article/details/78471819