50b5ac6bd376001c10b28e19bbde48da

来自「用java编写的程序」· 代码 · 共 50 行

TXT
50
字号
package webbook.beanutils.example;

import java.util.Map;
import java.util.HashMap;
import java.util.GregorianCalendar;
import org.apache.commons.beanutils.BeanUtils;

public class BeanUtilsExample1 {
	private User prepareData() {
		Profile profile = new Profile();
		profile.setEmail("javase@v512.com");
		profile.setBirthDate(new GregorianCalendar(2040, 9, 10).getTime());
		Map<String, String> phone = new HashMap<String, String>();
		phone.put("home", "11011011");
		phone.put("office", "82826905");
		profile.setPhone(phone);
		Address[] address = { new Address("中国", "北京", "100120", "天安门北大街888号"),
				new Address("中国", "广州", "100120", "石牌村666号") };
		profile.setAddress(address);

		User user = new User();
		user.setUserId(new Long(123456789));
		user.setUsername("刘伟");
		user.setPassword("ingodwetrust");
		user.setProfile(profile);
		return user;
	}

	public static void main(String[] args) {
		BeanUtilsExample1 example = new BeanUtilsExample1();
		User user = example.prepareData();
		try {
			System.out.println(BeanUtils.getProperty(user, "userId"));
			System.out.println(BeanUtils.getProperty(user, "username"));
			System.out.println(BeanUtils.getProperty(user, "password"));

			System.out.println(BeanUtils.getProperty(user, "profile.email"));
			System.out.println(BeanUtils.getProperty(user, "profile.birthDate"));
			System.out.println(BeanUtils.getProperty(user, "profile.phone(home)"));
			System.out.println(BeanUtils.getProperty(user, "profile.phone(office)"));
			System.out.println(BeanUtils.getProperty(user, "profile.address[0]"));
			System.out.println(BeanUtils.getProperty(user, "profile.address[1]"));
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?