利用BeanUtils将数据封装为javabean
2017-08-14 本文已影响21人
LOOK_LOOK
1.导包
commons-beanutils-1.8.3.jar
commons-logging-1.1.1.jar
2.封装
/**
* 1.获取参数集(jsp页面的input的name要和javabean的属性名对应)
* 2.调用方法分装成javabean(自动转型)
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/**
* 1.获取参数集(jsp页面的input的name要和javabean的属性名对应)
*/
Map<String, String[]> map = new HashMap<String, String[]>(
request.getParameterMap());
User user = new User();
try {
/**
* 2.调用方法分装成javabean
*/
BeanUtils.populate(user, map);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
User类
jsp表单部分