登录案例_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);
}
}
}