BeanUtils是一種非常實用的JavaBean,在apache底下的一個組件,我稍微介紹一下它的實用性,等等用程式碼來代表就清楚了,首先需要先把 commons-beanutils-1.8.3.jar 和 commons-logging-1.1.1.jar 導入,再將bean屬性建置好
public class User {
private String username;
private String password;
private String attribute;
private Long userId;
private Profile profile;
//以下get,set
}
public class Profile {
private String email;
private Date birthDate;
private Address[] address;
private Map<string, string=""> phone;
private HashMap telephone;
//以下get,set
}
public class Address {
private String country;
private String city;
private String addr;
private String postcode;
public Address(String country, String city, String addr, String postcode) {
this.country = country;
this.city = city;
this.addr = addr;
this.postcode = postcode;
}
//以下get,set
}
import java.lang.reflect.InvocationTargetException;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
import BeanUtils.Address;
import BeanUtils.Profile;
import BeanUtils.User;
public class BeanUtilExample {
//建立假資料
public User prepareDate() throws IllegalAccessException, InvocationTargetException {
Profile profile = new Profile();
profile.setEmail("pp@gmail.com");
profile.setBirthDate(new GregorianCalendar(2012, 06, 16).getTime());
Map<string, string=""> phone = new HashMap<string, string="">();
phone.put("home", "(03)XXXXXXX");
phone.put("office", "(02)1234-5678");
phone.put("mobile", "09XX-XXX-XXX");
profile.setPhone(phone);
Address addr1 = new Address("台灣省", "桃園縣中壢市", "XX路", "100001");
Address addr2 = new Address("台灣省", "台北市中山區", "XXX路", "100002");
Address[] address = { addr1, addr2 };
profile.setAddress(address);
User user = new User();
user.setUserId(new Long(123456789));
user.setUsername("puma");
user.setPassword("987654321");
user.setAttribute("男");
user.setProfile(profile);
return user;
}
public static void main(String[] args) {
BeanUtilExample example = new BeanUtilExample();
try {
User user = example.prepareDate();
//取值方式
System.out.println(BeanUtils.getProperty(user, "userId"));
System.out.println(PropertyUtils.getProperty(user, "userId"));
System.out.println(BeanUtils.getProperty(user, "username"));
System.out.println(BeanUtils.getProperty(user, "password"));
System.out.println(BeanUtils.getProperty(user, "attribute"));
System.out.println(BeanUtils.getProperty(user, "profile"));
System.out.println(BeanUtils.getProperty(user, "profile.email"));
System.out.println(BeanUtils.getProperty(user, "profile.birthDate"));
System.out.println(BeanUtils.getProperty(user, "profile.phone" ));
System.out.println(BeanUtils.getProperty(user, "profile.phone(mobile)" ));
System.out.println(BeanUtils.getProperty(user, "profile.phone(office)" ));
System.out.println(BeanUtils.getProperty(user, "profile.phone(home)" ));
System.out.println(BeanUtils.getProperty(user, "profile.address[0].country" ));
System.out.println(BeanUtils.getProperty(user, "profile.address[0].city" ));
System.out.println(BeanUtils.getProperty(user, "profile.address[0].postcode" ));
System.out.println("--------------------------------------------------------------------");
User user2 = new User();
BeanUtils.copyProperties(user2, user); //將user這物件轉至user2
System.out.println(BeanUtils.getProperty(user2, "userId" )); //輸出user類的userId的值
System.out.println(PropertyUtils.getProperty(user2, "userId" ));
System.out.println(BeanUtils.getProperty(user2, "username" ));
System.out.println(BeanUtils.getProperty(user2, "password" ));
System.out.println(BeanUtils.getProperty(user2, "profile" ));
System.out.println(BeanUtils.getProperty(user2, "profile.email" ));
System.out.println(BeanUtils.getProperty(user2, "profile.birthDate" ));
System.out.println(BeanUtils.getProperty(user2, "profile.phone" ));
System.out.println(BeanUtils.getProperty(user2, "profile.phone(mobile)" ));
System.out.println(BeanUtils.getProperty(user2, "profile.phone(office)" ));
System.out.println(BeanUtils.getProperty(user2, "profile.phone(home)" ));
System.out.println(BeanUtils.getProperty(user2, "profile.address[0].country" ));
System.out.println(BeanUtils.getProperty(user2, "profile.address[0].city" ));
System.out.println(BeanUtils.getProperty(user2, "profile.address[0].postcode" ));
System.out.println("--------------------------------------------------------------------");
Profile profile = new Profile();
HashMap map = new HashMap();
map.put( "1", "13880808080" );
map.put( "2", "13550505050" );
BeanUtils.setProperty( profile, "telephone", map );
System.out.println(BeanUtils.getProperty( profile, "telephone(1)" ));
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
- 首頁
- JAVA 目錄
- JAVA WEB 目錄
- JAVA 其他未分類 目錄
- Grails目錄
- jQuery 目錄
- Apache 目錄
- JavaScript 目錄
- Spring + Hibernate 目錄
- Hibernate 目錄
- Linux 目錄
- 程式開發工具目錄
- MySQL 目錄
- PHP目錄
- C/C++ 目錄
- Google App Engine 目錄
- HTML5/CSS3
- 程式開發基本資訊
- Android
- Oracle 目錄
- Struts 目錄