BeanUtils的简单使用

使用BeanUtils的原因

这里写图片描述

因为setProperty是JSP中的标签,因此使用model 2模式JSP+Servlet+JavaBean的时候,JSP将form提交给Servlet程序,而Servlet程序无法像JSP一样使用setProperty对数据进行封装,

<jsp:useBean id="calculator" class="cn.megustas.javabean.Calculator" scope="page"/>
<jsp:setProperty property="*" name="calculator"/>
  • 1
  • 2

但是可以通过内省技术进行数据的封装,但是每一次写内省程序是一件很麻烦的事情,并且内省匹配也会有问题(例如一侧是String,一侧是int,还需要进行数据转化),因此,内省(基于反射,方便操作javabean的API)封装form数据到javabean的代码,一般不自己编写,使用已经编写好的工具开发包BeanUtils开发包。

注意:如果想自动封装数据,表单form中元素的name需要与JavaBean中的属性一致。

什么是BeanUtils

BeanUtils一套开发包,Apache公司提供 ,专门进行javabean操作,在web层各种框架中被使用,例如:struts 使用BeanUtils操作JavaBean 。

实例 
1、下载BeanUtils的jar :commons-beanutils 、commons-logging,需要同时下载两个jar包。(BeanUtils依赖Logging的jar包 ) 
2、将beanutils和logging的 jar包复制 工程/WebContent/WEB-INF/lib 
这里写图片描述

JavaBean

package cn.megustas.javabean;



/**
 * 必须满足 三个条件 :必须满足 三个条件 :无参数构造器(不创建有参数构造器,即会有系统默认的无参构造器)、private成员,public get/set
 * 
 * @author Megustas
 * 
 */
public class Person {
    private String name;
    private String city;
    private String hobby;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public void setHobby(String hobby) {
        this.hobby = hobby;
    }

    public String getHobby() {
        return hobby;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getAge() {
        return age;
    }



}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51

JSP页面

<body>
<!-- 使用BeanUtils将form参数封装到 JavaBean Person对象中 -->
<!-- "/megustas/demo"为封装数据的Servlet的url -->
<form action="/megustas/demo" method="post">
    姓名 <input type="text" name="name" /> <br/>
    城市 <input type="text" name="city" /> <br/>
    爱好 <input type="text" name="hobby" /> <br/>
    年龄 <input type="text" name="age" /><br/>
    <input type="submit" value="提交" /><!-- 点击提交,提交的数据即是类中需要的属性 -->
</form>
</body>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

通过action将form表单提交给servlet程序,对数据进行封装,封装到Person对象中 
Servlet程序(映射中的url为/megustas/demo):

package cn.megustas.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;

import cn.itcast.javabean.Person;

/**
 * 获得form数据 ,将form数据封装 Person对象中
 * 
 * @author Megustas
 * 
 */
public class DemoServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");// 处理乱码

        // 将form参数 自动封装 Person对象
        Person person = new Person();
        try {
            BeanUtils.populate(person, request.getParameterMap());
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        //检验是否封装到person对象中
        System.out.println(person.getName());
        System.out.println(person.getCity());
        System.out.println(person.getHobby());
        System.out.println(person.getAge());
        System.out.println(person.getBirthday());
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48

注意:

  • 引BeanUtils包时要引org.apache.commons.beanutils
BeanUtils原理

常用方法:

//将请求的Map封装为Person对象(JavaBean)
BeanUtils.populate(Object bean,Map properties);
  • 1
  • 2

(注:因为需要为Map类型,因此需要通过request.getParameterMap()方法将form表单中的数据变为Map类型)

这里写图片描述

实际上是通过JavaBean中的setName等方法,将form表单中的属性的值封装到person对象中去。对于类型转换,我们可以看下如下的这个例子,我们在JavaBean中添加一个属性Date(对应set/get方法也需要添加):

public class Person {
    private String name;
    private String city;
    private String hobby;
    private int age;
    private Date birthday;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public void setHobby(String hobby) {
        this.hobby = hobby;
    }

    public String getHobby() {
        return hobby;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getAge() {
        return age;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public Date getBirthday() {
        return birthday;
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48

JSP页面中增加:

    生日 <input type="text" name="birthday" /><br/>
  • 1

Servlet程序中增加一个输出

System.out.println(person.getBirthday());
  • 1

则将会报错,因为服务器端为Date类型,从输入的form表单的数据无法转换过去,即如果在Person类添加特殊类型Date类型,对于特殊类型如何封装 ?因此,需要了解一下BeanUtils的封装原理

这里写图片描述

即任何的类型转换都是通过转换器来实现的。例如我们之前的例子,String类型的提交数据是通过IntegerConverter转换器转换为person对象中的int类型的。可以总结为如下:

1、如果form参数是String类型,javabean属性String类型 —- 调用BeanUtils.populate 将form数据封装 javabean 
2、如果JavaBean属性类型不是String —- 将form数据封装javabean 需要转换器 —– 在BeanUtils API 提供很多默认转换器(完成常见转换) 
3、如果需要转换类型非常特殊,可以通过自定义转换器完成 定义类实现Converter接口,实现convert方法 ,在populate调用之前通过ConvertUtils.register注册转换器

因此,如果出现上种情况,即需要将String类型转换为java.util.Date类型,我们可以自定义转换器

自定义转换器(或者在已有转换器,但是不知道如何使用的情况也可以通过自定义转换器来进行类型转换): 
1.需要实现Converter接口

class MyDateConverter implements Converter {

    @Override
    // 需要 将value数据转换 c的类型
    // value 是 String类型,将String转换Date类型
    public Object convert(Class c, Object value) {
        String s = (String) value; // 代表用户输入生日
        //定义格式
        DateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日");
        try {
            Object result = dateFormat.parse(s); // 将String解析为 Date,将日期变为字符串使用format方法
            return result;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

因为在转换器中定义了“yyyy年MM月dd日”的格式,因此在jsp页面的form表单需要输入信息为“1998年2月12日”这类的格式的字符串,才可以通过我们自定义的转换器进行转换,转换为Date类型并之后进行存储。 
2.注册转换器

ConvertUtils.register(转换器, 目标类型.class);
  • 1

修改之后的Servlet程序:

public class Demo4Servlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");// 处理乱码

        // 将form参数 自动封装 Person对象
        Person person = new Person();

        // 将String类型 转换 java.util.Date类型 --- 自定义转换器
        // 在封装数据之前 ,注册转换器
        ConvertUtils.register(new MyDateConverter(), Date.class);

        try {
            BeanUtils.populate(person, request.getParameterMap());
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        //检验是否封装到person对象中
        System.out.println(person.getName());
        System.out.println(person.getCity());
        System.out.println(person.getHobby());
        System.out.println(person.getAge());
        System.out.println(person.getBirthday());
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
BeanUtils结论
  1. BeanUtils.populate(Object bean, Map properties)方法进行数据封装
  2. 一般其实不需要自定义转换器,只有在特殊情况下才会自定义转换器 
    1、如果form参数是String类型,javabean属性String类型 —- 调用BeanUtils.populate 将form数据封装 javabean 
    2、如果JavaBean属性类型不是String —- 将form数据封装javabean 需要转换器 —– 在BeanUtils API 提供很多默认转换器(完成常见转换) 
    3、如果需要转换类型非常特殊,可以通过自定义转换器完成 定义类实现Converter接口,实现convert方法 ,在populate调用之前通过ConvertUtils.register注册转换器

最通俗的说法,JavaBean就是一个存数据的地方,要么 
(1)通过JSP页面中的标签去存储form表单中的数据(action跳转到JSP页面,之后“useBean”,“setProperty”进行数据存储),再在JSP界面中通过“getProperty”显示数据。 
(2)在JSP页面中通过action跳转到Servlet程序的url,在Servlet程序中通过BeanUtils工具类的相关方法对JSP页面中的表单信息进行存储。

JSP+Servlet+JavaBean模式示意图:

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值