JavaBean

释放双眼,带上耳机,听听看~!

概述:Java开发中常用的组件,其实就是一个Java类,他的作用是封装数据

五个规范:

1,这个Java类,被public修饰
2,这个类要提供公共无参构造方法
3,要提供私有的属性
4,要给私有的属性提供公共的set或者get方法
5,要实现serializable接口(非必须)

public class Demo_javabean implements Serializable{

    public Demo_javabean() {

    }
    private String ID = null;
    private String name = null;
    public String getID() {
        return ID;
    }
    public void setID(String iD) {
        ID = iD;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

Javabean中属性和成员变量有点区别,属性是getxxx方法中字段xxx的小写。

public class Demo_javabean implements Serializable{

    public Demo_javabean() {

    }
    //成员变量是ID
    private String ID = null;
    //成员变量是name
    private String name = null;
    //属性是id
    public String getID() {
        return ID;
    }
    public void setID(String iD) {
        ID = iD;
    }
    //属性是name
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

BeanUtils工具

BeanUtils 工具时由apache提供的一套封装数据到javabean的工具类。

需要用到的jar包,放到lib目录下

  • commons-beanutils-1.8.3.jar
  • commons-logging-1.2.jar

常用API:

1,设置值:setproperty(javabean对象,javabean属性,javabean属性值)
2,获取值:getproperty(javabean对象,javabean属性)
3,向javabean属性设置值:populate(javabean对象,map对象)
注意:map对象的key(就是javabean属性名称)必须保持一致。

public class Demo_javabean{

    public Demo_javabean() {
        super();
    }
    //成员变量是ID
    private String ID = null;
    //成员变量是name
    private String name = null;
    //属性是id
    public String getID() {
        return ID;
    }
    public void setID(String iD) {
        ID = iD;
    }
    //属性是name
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}
public static void main(String[] args) throws Exception {
        Demo_javabean db = new Demo_javabean();
        //设置属性
        BeanUtils.setProperty(db, "ID","100");
        BeanUtils.setProperty(db, "name", "lucy");
        //获取属性
        String id = BeanUtils.getProperty(db, "ID");
        String name = BeanUtils.getProperty(db, "name");
        //BeanUtils.populate(db, Map<id,"666">);
        System.out.println(id);
        System.out.println(name);
        System.out.println("=======================");
        HashMap<String, Object> map = new HashMap<>();
        map.put("ID", "22");
        map.put("name", "wd");
        BeanUtils.populate(db,map);
        id = BeanUtils.getProperty(map, "ID");
        name = BeanUtils.getProperty(map, "name");
        System.out.println(id);
        System.out.println(name);


    }

在提交表单过程中普通封装和Javabean封装的区别。

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String ID = request.getParameter("ID");
        String name = request.getParameter("name");

        //传统方式
        System.out.println("======传统方式======");
        Demo_javabean dj = new Demo_javabean();
        dj.setID(ID);
        dj.setName(name);
        System.out.println(dj);

        System.out.println("===========javabean方式===========");
        Demo_javabean dj_2 = new Demo_javabean();
        try {
            BeanUtils.populate(dj_2, request.getParameterMap());
            //response.getWriter().print(dj);        
        } catch (IllegalAccessException | InvocationTargetException e) {
    
            e.printStackTrace();
        }
    }

用法

通过beanutils对象进行request提交的数据封装,不再需要逐条信息封装,只需要通过一个方法一次性封装即可。但这里的前提就是,表单提交信息的字段名与封装对象的属性名(不是成员对象名)要一致。如果不一致,将会是获取到null值。

只要确保user的属性name与表单提交的name字段,以及user对象的属性password与password字段名相同,就可以通过beanutils来获取了。

package p02_user;

public class User {
    private String Name;
    private String Password;

    public String getName() {
        return Name;
    }
    public void setName(String name) {
        Name = name;
    }
    public String getPassword() {
        return Password;
    }
    public void setPassword(String password) {
        Password = password;
    }
    @Override
    public String toString() {
        return "User [Name=" + Name + ", Password=" + Password + "]";
    }




}
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
</head>
<body>
    <form action="/Demo_02_BeanUtils/BeanUtilsServlet" method="post">
        <table border="1">
            <tr>
                 <td>账号:<input type="text" name="username"></td>
            </tr>
            <tr>
                <td>密码:<input type="password" name="password"></td>
            </tr>
            <tr>
                <td><input type="submit" value="提交"></td>
            </tr>
        </table>
    </form>
</body>
</html>
package p01_servlet;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

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 p02_user.User;
public class BeanUtilsServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    public BeanUtilsServlet() {
        super();
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User u = new User();
        try {
            BeanUtils.populate(u, request.getParameterMap());
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        response.getWriter().print(u);;
    }

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

}

给TA买糖
共{{data.count}}人
人已赞赏
知识分享

计算机网络体系结构

2020-9-18 22:29:00

知识分享

OS内核功能大全

2020-9-18 22:32:00

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索