目录:
1.什么是BeanUtils组件
2.导入jar包
3.如何使用BeanUtils
4.在Servelt中使用BeanUtils组件处理请求的封装
1.什么是BeanUtils组件?
BeanUtils是apache提供的一套开源的api,方便对javabean进行操作
2.导入jar包
1.引入:commons-beanutils-1.8.3.jar 核心包
2.引入:commons-logging-1.1.3.jar 日志包
百度云链接: https://pan.baidu.com/s/1c2vhE3u 密码: d6x9
3.如何使用BeanUtils
- 首先要写一个javaBean文件
package com.huan.beans;
import java.util.Date;
/**
* Created by pc on 17-5-10.
*/
public class Admin {
private String username;
private String password;
private int age;
private Date brith;
public Date getBrith() {
return brith;
}
public void setBrith(Date brith) {
this.brith = brith;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
1).对象属性的拷贝
对基本数据类型,会自动进行类型转换
package com.huan.beans;
import org.apache.commons.beanutils.BeanUtils;
import org.junit.Test;
import java.lang.reflect.InvocationTargetException;
/**
* Created by pc on 17-5-10.
*/
public class App {
@Test
public void test () throws Exception {
//基本实现
Admin admin = new Admin();
// admin.setUsername("mahuan");
// admin.setPassword("123456");
//BeanUtils组件实现对象属性的拷贝(两种写法)
BeanUtils.copyProperty(admin,"username","mahuan");
BeanUtils.setProperty(admin,"password","123456");
System.out.println(admin.getUsername());
System.out.println(admin.getPassword());
}
}
2).对象的拷贝
package com.huan.beans;
import org.apache.commons.beanutils.BeanUtils;
import org.junit.Test;
import java.lang.reflect.InvocationTargetException;
/**
* Created by pc on 17-5-10.
*/
public class App {
@Test
public void test () throws Exception {
//基本实现
Admin admin = new Admin();
// admin.setUsername("mahuan");
// admin.setPassword("123456");
//BeanUtils组件实现对象属性的拷贝
BeanUtils.copyProperty(admin,"username","mahuan");
BeanUtils.setProperty(admin,"password","123456");
//拷贝对象
Admin newAdmin = new Admin();
BeanUtils.copyProperties(newAdmin,admin );
System.out.println(newAdmin.getUsername());
System.out.println(newAdmin.getPassword());
}
}
3).map数据拷贝到对象中
map中key要和javaBean中的属性一致
@Test
public void test2() throws Exception {
Admin adminMap = new Admin();
Map<String,Object> map = new HashMap<String,Object>();
map.put("username","xiaoming");
map.put("age",22);
BeanUtils.populate(adminMap,map);
System.out.println(adminMap.getUsername());
System.out.println(adminMap.getAge());
}
0
4).日期类型转换器
- (1)自定义
@Test
public void test3() throws Exception {
String name = "xiaohuan";
int age = 20;
String birth = "1995-09-09";
//注册日期类转换器
//方式1.自定义
ConvertUtils.register(new Converter() {
public Object convert(Class aClass, Object o) {
if (aClass != Date.class) {
return null;
}
if (o == null || "".equals(o.toString().trim())) {
return null;
}
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.parse(o.toString());
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
}, Date.class);
Admin adminData = new Admin();
BeanUtils.copyProperty(adminData, "username", name);
BeanUtils.copyProperty(adminData, "age", age);
BeanUtils.copyProperty(adminData, "brith", birth);
System.out.println(adminData.getBrith());
}
- (2)使用组件实现日期类型转换器
@Test
public void test3() throws Exception {
String name = "xiaohuan";
int age = 20;
String birth = "1995-09-09";
//注册日期类转换器
//方式1.使用组件
ConvertUtils.register(new DateLocaleConverter(),Date.class);
Admin adminData = new Admin();
BeanUtils.copyProperty(adminData, "username", name);
BeanUtils.copyProperty(adminData, "age", age);
BeanUtils.copyProperty(adminData, "brith", birth);
System.out.println(adminData.getBrith());
}
image.png
4.在Servelt中使用BeanUtils组件处理请求的封装
- 在servlet中调用封装好的数据处理
Admin admin = wecUtils.copyToBean(request,Admin.class);
- 新建webUtils.java文件
package com.huan.beans;
import org.apache.commons.beanutils.BeanUtils;
import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
/**
* Created by pc on 17-5-10.
*/
public class WebUtils {
public static <T> T copyToBean(HttpServletRequest request, Class<T> tClass){
try {
T t = tClass.newInstance();
Enumeration<String> enums = request.getParameterNames();
while (enums.hasMoreElements()) {
String name = enums.nextElement();
String value = request.getParameter(name);
BeanUtils.copyProperty(t, name, value);
}
}catch (Exception e){
throw new RuntimeException(e);
}
return null;
}
}