BeanUtils的介绍与使用

(1)BeanUtils工具由Apache软件基金组织编写,提供给我们使用,主要解决的问题是:把对象的属性数据封装到对象中。

使用BeanUtils前提导入jar包到项目中,

BeanUtils的介绍与使用

下图是表单的一部分代码设计:注意将下面的RequestServlet换成BeanUtilsServlet

BeanUtils的介绍与使用

BeanUtils的介绍与使用

BeanUtils的介绍与使用

BeanUtils的介绍与使用

在BeanUtilsServlet中的代码



protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     //创建一个空对象
Student student=new Student();
//获取参数的集合
try {
//直接通过BeanUtils的静态方法将请求参数的集合赋值进去,对对象进行赋值。
BeanUtils.populate(student,request.getParameterMap());

}catch(IllegalAccessException e) {
e.printStackTrace();
}catch(InvocationTargetException e) {
e.printStackTrace();

}
System.out.println(student.toString());

}


/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

doGet(request, response);

}

还要在web.xml 中对其进行配置