目錄:
1.什么是BeanUtils組件
2.導(dǎo)入jar包
3.如何使用BeanUtils
4.在Servelt中使用BeanUtils組件處理請(qǐng)求的封裝
1.什么是BeanUtils組件蚕捉?
BeanUtils是apache提供的一套開(kāi)源的api,方便對(duì)javabean進(jìn)行操作
2.導(dǎo)入jar包
1.引入:commons-beanutils-1.8.3.jar 核心包
2.引入:commons-logging-1.1.3.jar 日志包
百度云鏈接: https://pan.baidu.com/s/1c2vhE3u 密碼: d6x9
3.如何使用BeanUtils
- 首先要寫(xiě)一個(gè)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).對(duì)象屬性的拷貝
對(duì)基本數(shù)據(jù)類(lèi)型,會(huì)自動(dòng)進(jìn)行類(lèi)型轉(zhuǎn)換
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 {
//基本實(shí)現(xiàn)
Admin admin = new Admin();
// admin.setUsername("mahuan");
// admin.setPassword("123456");
//BeanUtils組件實(shí)現(xiàn)對(duì)象屬性的拷貝(兩種寫(xiě)法)
BeanUtils.copyProperty(admin,"username","mahuan");
BeanUtils.setProperty(admin,"password","123456");
System.out.println(admin.getUsername());
System.out.println(admin.getPassword());
}
}
2).對(duì)象的拷貝
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 {
//基本實(shí)現(xiàn)
Admin admin = new Admin();
// admin.setUsername("mahuan");
// admin.setPassword("123456");
//BeanUtils組件實(shí)現(xiàn)對(duì)象屬性的拷貝
BeanUtils.copyProperty(admin,"username","mahuan");
BeanUtils.setProperty(admin,"password","123456");
//拷貝對(duì)象
Admin newAdmin = new Admin();
BeanUtils.copyProperties(newAdmin,admin );
System.out.println(newAdmin.getUsername());
System.out.println(newAdmin.getPassword());
}
}
3).map數(shù)據(jù)拷貝到對(duì)象中
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());
}
4).日期類(lèi)型轉(zhuǎn)換器
- (1)自定義
@Test
public void test3() throws Exception {
String name = "xiaohuan";
int age = 20;
String birth = "1995-09-09";
//注冊(cè)日期類(lèi)轉(zhuǎn)換器
//方式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)使用組件實(shí)現(xiàn)日期類(lèi)型轉(zhuǎn)換器
@Test
public void test3() throws Exception {
String name = "xiaohuan";
int age = 20;
String birth = "1995-09-09";
//注冊(cè)日期類(lèi)轉(zhuǎn)換器
//方式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());
}
4.在Servelt中使用BeanUtils組件處理請(qǐng)求的封裝
- 在servlet中調(diào)用封裝好的數(shù)據(jù)處理
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;
}
}