BeanUtils工具类封装对象的使用

要想使用BeanUtils工具类封装对象,必须使用三个jar包(缺一不可):
百度网盘下载地址:BeanUtils的三个jar包
提取码:pqhm
在这里插入图片描述
使用BeanUtils封装javaBean对象:`
 1)创造一个需要的javaBean对象

import java.io.Serializable;
public class User implements Serializable {
    private Integer uId; //ID
    private String username;  //用户名
    private String password;  //密码
    public User() {
    }

    public User(Integer uId, String username, String password) {
        this.uId = uId;
        this.username = username;
        this.password = password;
    }

    public Integer getuId() {
        return uId;
    }

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }

    public void setuId(Integer uId) {
        this.uId = uId;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "User{" +
                "uId=" + uId +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

 2)调用BeanUtils.populate(需要封装的javaBean对象,request.getParameterMap());

 Map<String, String[]> map = req.getParameterMap();
        User user = new User();
        try {
            BeanUtils.populate(user,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

 3)就会通过map键中对应的key的set方法进行注入

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登陆页面</title>

    <style type="text/css">
        *{
            margin: 0 auto;
            padding: 0px;
        }

        #container{
            width: 500px;
            height: 500px;
            border: 1px solid gainsboro;
            margin-top: 20px;
        }

        input{
            width: 300px;
            height:40px;
            border-radius: 5px;
            border: 1px solid #8c8c8c;
        }

        #login{
            background-color: blue;
            font-size: 18px;
            color: white;
        }
        #myForm{
            text-align: center;
        }

        #title{
            font-size: 25px;
            font-weight: bold;
        }

        #tip{
            width: 100%;
            height: 50px;
            background-color: #dff0d8;
            margin-top: 30px;
            text-align: center;
            line-height: 50px;
            font-size: 15px;
            color: red;
            font-family: Centaur;
        }
    </style>

    <script src="js/jquery-3.3.1.js"></script>

    <script type="text/javascript">
        //切换验证码
        function refreshCode() {
            //获取验证码对象
            var vcode = document.getElementById("vcode");
            vcode.src="${pageContext.request.contextPath}/checkCodeServlet?time="+new Date().getTime();
        }
    </script>
</head>
<body>

    <div id="container">
        <div id="myForm">
            <form action="${pageContext.request.contextPath}/loginServlet" method="post">
                <span id="title">用户登陆</span><br><br>
                <p>
                    <span>用户名:</span> <input type="text" name="username">
                </p><br><br>
                <p>
                    <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;码:</span><input type="password" name="password">
                </p><br><br>
                <p>
                    <span>验证码:</span>
                    <input type="text" name="checkCode" ><br>
                    <a href="javascript:refreshCode();">
                        <img src="${pageContext.request.contextPath}/checkCodeServlet" id="vcode" title="看不清?点击换一张">
                    </a>
                </p><br><br>
                <p>
                    <input type="submit" value="登陆" id="login">
                </p>
            </form>
        </div>
        <div id="tip">${login_msg}</div>
    </div>
</body>
</html>


注意要想将jsp页面中表单的数据封装到javaBean对象中,需要有一个name属性,且name属性的值最好要与javaBean对象中属性的值对应。

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值