登录案例_BeanUtils基本使用与登录案例_BeanUtils介绍

登录案例_BeanUtils基本使用 

 

    BeanUtils工具类,简化数据封装

  

复制代码
 //2.获取所有请求参数
        Map<String, String[]> map = request.getParameterMap();
        //3.创建User对象
        User loginuser = new User();
        //3.2使用BeanUtils封装
        try {
            BeanUtils.populate(loginuser,map);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
复制代码

登录案例_BeanUtils介绍

    BeanUtils介绍

       1.用于封装JavaBean的  

          1.JavaBean:标准的java类

            1.要求

              1.类必须被public修饰

              2.必须提供空参的构造器

              3.成员变量必须使用private修饰

              4.提供公共settter和getter方法

              2.功能:封装数据

       2.概念:

           成员变量:

           属性:setter和getter方法截取后的产物

              例如:getUsername()-->  Username -->username

 

        3.方法:

          1.setProperty()

          2.getProperty()

          3.populate:

 

复制代码
package com.example.test;

import com.example.domain.User;
import org.apache.commons.beanutils.BeanUtils;
import org.junit.Test;

import java.lang.reflect.InvocationTargetException;

public class BeanUtilsTest {

    @Test
    public void test(){


        User user = new User();

        try {
            BeanUtils.setProperty(user,"username","caixukun");
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }

        System.out.println(user);

    }
}
复制代码

 

 

 

 

    

 

 

 

      

          

 

复制代码
package com.example.test;


import com.example.domain.User;
import org.apache.commons.beanutils.BeanUtils;
import org.junit.Test;

import java.lang.reflect.InvocationTargetException;

public class BeanUtilsTest {

    @Test
    public void test(){

        User user = new User();

        try {
            BeanUtils.setProperty(user,"hehe","male");
            System.out.println(user);


            String gender = BeanUtils.getProperty(user, "hehe");
            System.out.println(gender);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        }


    }
}
复制代码

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

驸马
驸马爷近前看端详
上写着秦香莲她三十二岁
状告当朝驸马郎
欺君王 瞒皇上
悔婚男儿招东床
杀妻灭子良心丧
他逼死韩琪在庙堂
将状纸压至在了某的大堂上
咬紧了牙关你为哪桩

 



package com.example.test;


import com.example.domain.User;
import org.apache.commons.beanutils.BeanUtils;
import org.junit.Test;

import java.lang.reflect.InvocationTargetException;

public class BeanUtilsTest {

@Test
public void test(){

User user = new User();

try {
BeanUtils.setProperty(user,"hehe","male");
System.out.println(user);


String gender = BeanUtils.getProperty(user, "hehe");
System.out.println(gender);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}


}
}
posted @   zj勇敢飞,xx永相随  阅读(28)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示