/**
* 把request中的所有键值赋到bean
*
* @throws InvocationTargetException
* @throws IllegalAccessException
*/
@SuppressWarnings("unchecked")
public static void populateReqBean(Object bean, HttpServletRequest request) {
HashMap map = new HashMap();
Enumeration names = request.getParameterNames();
while (names.hasMoreElements()) {
String name = (String) names.nextElement();
map.put(name, request.getParameterValues(name));
}
try {
BeanUtils.populate(bean, map);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
本文介绍了一种将HTTP请求参数填充到Java Bean的方法。通过枚举请求参数名称并将它们存储到HashMap中,再使用Apache Commons BeanUtils的populate方法将Map中的键值对映射到指定的Bean对象上。
2374

被折叠的 条评论
为什么被折叠?



