-----------内省(introspection)------------- 1. javabean 遵循固定写法的java类 1)必须有无参构造函数 Class.forName().newInstance() 2)属性必须私有,我们称为字段 3)提供标准的getter和setter 例: name 字段 的getter: String getName() settter: void setName(String name) 判断一个javabean是否有name属性取决于 是否有getName 或 setName 方法 2. 内省 通过反射的方式访问javabean,我们称之为内省 BeanUtils setProperty(bean, name, value) copyProperties(target, source); 可以支持String到8中基本数据类型转换 其他引用数据类型都需要注册转换器 ConvertUtils.register(Converter, Class) 3. WebUtils 工具类 实现任何request提交的表单封装到对应的javabean 需要使用的jar包: 1) http://commons.apache.org/logging/download_logging.cgi 2) http://logging.apache.org/log4j/1.2/download.html 3) http://commons.apache.org/beanutils/download_beanutils.cgi -----------示例---------- public static void main(String[] args) throws Exception { // 使用 BeanUtils 做内省 Person bean = new Person(); String name = "zhangsan"; String age = "19"; // Integer.parseInt() String isMarried = "true"; String birthday = "1980-3-3"; // 注册一个转换器 作用 : 将一种类型转为另一种类型 ConvertUtils.register(new DateLocaleConverter(Locale.CHINA), Date.class); BeanUtils.setProperty(bean, "name", name); BeanUtils.setProperty(bean, "age", age); BeanUtils.setProperty(bean, "married", isMarried); BeanUtils.setProperty(bean, "country", "china"); BeanUtils.setProperty(bean, "birthday", birthday); System.out.println(bean); System.out.println(bean.getBirthday());//Mon Mar 03 00:00:00 CST 1980 } ----------Person.java------------------- public class Person { private String name; // 字段 private int age; private boolean isMarried; private String Country; private Date birthday; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public boolean isMarried() { return isMarried; } public void setMarried(boolean isMarried) { this.isMarried = isMarried; } public String getCountry() { return Country; } public void setCountry(String country) { Country = country; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } } 转自:http://blog.sina.com.cn/s/blog_7068e4e40100z1i8.html