beanutils.Converter使用探索

org.apache.commons.beanutils.ConvertUtils

这是一个接口,其中只包含一个方法 convert (java.lang.Class type, java.lang.Object value) ,其功能是将输入参数 value 转换为指定类型 type 的实例。

范例:

我建立了一个 Rectangle 对象,并为这 rectangle 对象建立了一个 RectangleConverter ,随后将 RectangleConverter 注册进 ConvertUtils 中。形为 {3,2} 的字符串可以转变为一个 rectangle 

这里唯一要说明的就是 RectangleConverter 中要实现两个 converter 方法,也就是要对接口 Converter 中定义的方法进行 overload,从而可以实现字符串和 Rectangle 之间的互相转换了。

 

package beanutil; 
import java.util.StringTokenizer; 
import org.apache.commons.beanutils.ConvertUtils; 
import org.apache.commons.beanutils.Converter;

public class RectangleConverter implements Converter {

 

    public Object convert(Class arg0, Object arg1) { 
       if (arg1 instanceof String) 
                return convert(arg0, arg1.toString()); 
       return convert(arg0, (Rectangle) arg1);

   }

    public Rectangle convert(Class rectangleType, String para) { 
           StringTokenizer token = new StringTokenizer(para, ","); 
           Double temp1, temp2 = null; 
           temp1 = (Double) ConvertUtils.convert(token.nextToken(), Double.class); 
           temp2 = (Double) ConvertUtils.convert(token.nextToken(), Double.class); 
           Rectangle rect = new Rectangle(); 
            rect.setLength(Math.max(temp1.doubleValue(), temp2.doubleValue())); 
            rect.setWidth(Math.min(temp1.doubleValue(), temp2.doubleValue())); 
           return rect;

       }

 

 public String convert(Class string, Rectangle rect) {

       StringBuffer buffer = new StringBuffer("("); 
       buffer.append(rect.getLength()).append(","); 
        buffer.append(rect.getWidth()); 
        return buffer.toString();

       }

 

}

 

RectangleConverter注册到ConvertUtils中,我们就可以把形为{4,5}的字符串转换为Rectangle的实例,同样可以将Rectangle的实例转换为形为{4,5}的字符串。

 

另附Rectangle的代码:

package beanutil;

public class Rectangle {

 

       double length = 0.0d; 
       double width = 0.0d; 
        public double getLength() { 
            return length;

       }

 

       public void setLength(double length) { 
          this.length = length; 
      }

     public double getWidth() {

    return width;

     }

     public void setWidth(double width) {

            this.width = width; 
    }

 

 

 

 

      

 

 

 

 

       public double computeArea(){

 

 

 

 

              return length*width;

 

 

 

 

       }

 

 

 

 

      

 

 

 

 

       public String toString(){

 

 

 

 

              StringBuffer buffer = new StringBuffer("(");

 

 

 

 

              buffer.append(getLength()).append(",");

 

 

 

 

              buffer.append(getWidth()).append(")");

 

 

 

 

              return buffer.toString();

 

 

 

 

       }

 

 

 

 

}

}

















1)创建如下类:
package com.forever.util;

import java.util.*;
import org.apache.commons.beanutils.*;
import java.text.SimpleDateFormat;

public class DateConvert implements Converter {
static SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// HH:mm:ss

public DateConvert() { 


public Object convert(Class type, Object value) { 
if(value==null)return null; 
if(((String)value).trim().length()==0)
return null; 
if(value instanceof String) { 
try { 
//解析接收到字符串 
return df.parse((String)value); 
} catch (Exception ex) {
//发生解析异常
throw new ConversionException("输入的日期类型不合乎yyyy-MM-dd HH:mm:ss" + value.getClass()); 

} else { 
//其他异常 
throw new ConversionException("输入的不是字符类型" + value.getClass()); 

}
}

2)然后在你的系统某处使用如下(如web的init方法)
ConvertUtils.register(new DateConvert(),java.util.Date.class);
参数用于表示DateConvert类负责处理java.util.Date类型的转化。如:
package com.forever.util;

import org.apache.struts.action.ActionServlet;
import javax.servlet.ServletException;
import org.apache.commons.beanutils.ConvertUtils;

public class ActiveServlet extends ActionServlet {

/**
* 重载init()方法
* 为struts解析重新注册一些改造过的表单元素数据解析方法
*/
public void init() throws ServletException {
super.init();
ConvertUtils.register(new DateConvert(), java.util.Date.class);
}
}
3)在web.xml文件中加入或修改成如下内容:

< servlet >
< servlet-name >action< /servlet-name >
< servlet-class >com.forever.util.ActiveServlet< /servlet-class > 
< init-param >
< param-name >config< /param-name >
< param-value >/WEB-INF/struts-config.xml< /param-value >
< /init-param >
...........
< /servlet >

当用到BeanUtils.getProperty()时,碰到Date这种类型就发生转换

转载于:https://my.oschina.net/liting/blog/525032

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值