登录案例_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);
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)