本帖最后由 庄星睿 于 2012-9-12 13:06 编辑
- @Test
- public void test3() throws Exception {
- String name = "aaa";
- String password = "123";
- String age = "23";
- String birthday = "1980-09-09";
- Person p = new Person();
- ConvertUtils.register(new Converter() {
- @Override
- public Object convert(Class type, Object value) {
- if(value == null){
- return null;
- }
- if(!(value instanceof String)){
- throw new ConversionException("类型不匹配String");
- }
- String str = (String)value;
- if(str.trim().equals("")){
- return null;
- }
- SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
- try {
- return df.parse(str);
- } catch (ParseException e) {
- throw new RuntimeException(e);
- }
- }
- },Date.class);
- BeanUtils.setProperty(p, "name",name);
- BeanUtils.setProperty(p, "password",password);
- BeanUtils.setProperty(p, "age",age);
- BeanUtils.setProperty(p, "birthday",birthday); //String类型不能转换Date类型
- System.out.println(p.getName());
- System.out.println(p.getPassword());
- System.out.println(p.getAge());
- System.out.println(p.getBirthday());
- }
复制代码 排查了很久也没找到错误,转换器是没问题的,加了转换器,还是报
org.apache.commons.beanutils.ConversionException: DateConverter does not support default String to 'Date' conversion.
找到错误了,导包导错了,eclipse自动导入的,java.util.Date包导成了java.sql.Date,对初学者建议查错时如果报错信息看不出来,就先从导入的包中查找,排错太浪费时间了 |
|