person bean是这样的:- package cn.study.beanutils;
- import java.util.Date;
- public class Person {
- private String name;
- private int age;
- private String password;
- private Date birthday;
-
- public Date getBirthday() {
- return birthday;
- }
- public void setBirthday(Date birthday) {
- this.birthday = 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 String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
-
-
- }
复制代码 用BeanUtils的第三方类库操作Java bean:- @Test
- public void test2() throws Exception {
- Class clazz = Class.forName("cn.study.beanutils.Person");
- Person p = (Person) clazz.newInstance();
- ConvertUtils.register(new DateConverter(), Date.class);
-
- // setProperty
- //BeanUtils.setProperty(p, "name", "tome");
-
- BeanUtils.setProperty(p, "birthday", "1999-01-01");
- System.out.println(BeanUtils.getProperty(p, "birthday"));
-
- }
复制代码 运行时,有异常抛出,不知道为什么?- org.apache.commons.beanutils.ConversionException: DateConverter does not support default String to 'Date' conversion.
- at org.apache.commons.beanutils.converters.DateTimeConverter.toDate(DateTimeConverter.java:468)
- at org.apache.commons.beanutils.converters.DateTimeConverter.convertToType(DateTimeConverter.java:343)
- at org.apache.commons.beanutils.converters.AbstractConverter.convert(AbstractConverter.java:156)
- at org.apache.commons.beanutils.ConvertUtilsBean.convert(ConvertUtilsBean.java:470)
- at org.apache.commons.beanutils.BeanUtilsBean.setProperty(BeanUtilsBean.java:1006)
- at org.apache.commons.beanutils.BeanUtils.setProperty(BeanUtils.java:456)
- at cn.study.beanutils.Demo1.test2(Demo1.java:38)
- at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
- at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
- at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
- at java.lang.reflect.Method.invoke(Unknown Source)
- at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
- at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
- at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
- at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
- at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
- at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
- at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
- at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
- at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
- at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
- at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
- at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
- at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
- at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
- at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
- at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
- at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
- at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
复制代码 各位大侠,能帮忙看看问题吗?
|
|