重写org.apache.commons.beanutils.PropertyUtilsBean的copyPropert

在使用用struts的actionForm时要把属性拷到BO对象时发现 org.apache.commons.beanutils.PropertyUtilsBean的copyPropert有些问题:如不能把字符字符串转为Long类型(作为公共工具类本应是这样)为了适应开发需要我重写了copyPropert方法.下面贴出来
java 代码
  1. package com.zhgrd.basic.util;   
  2.   
  3. import java.beans.PropertyDescriptor;   
  4. import java.lang.reflect.InvocationTargetException;   
  5. import java.lang.reflect.Method;   
  6. import java.util.Iterator;   
  7. import java.util.Map;   
  8.   
  9. import org.apache.commons.beanutils.BeanUtilsBean;   
  10. import org.apache.commons.beanutils.ContextClassLoaderLocal;   
  11. import org.apache.commons.beanutils.DynaBean;   
  12. import org.apache.commons.beanutils.DynaProperty;   
  13. import org.apache.commons.beanutils.PropertyUtils;   
  14. import org.apache.commons.beanutils.PropertyUtilsBean;   
  15. import org.apache.commons.logging.Log;   
  16. import org.apache.commons.logging.LogFactory;   
  17. /**  
  18.  * 是基于org.apache.commons.beanutils的  
  19.  * 便于把form中的值拷到Bo对象中  
  20.  * 如Bo中没有的属性在form中存在则不拷贝,  
  21.  * 如果目标对象中的属性类型为Long型而拷贝对象为String则把String转为Long  
  22.  * @author ou  
  23.  *  
  24.  */  
  25. public class ObjectPropertyUtilsBean extends PropertyUtilsBean{   
  26.     Log log = LogFactory.getLog(ObjectPropertyUtilsBean.class);   
  27.        
  28.        
  29.      private static final ContextClassLoaderLocal beansByClassLoader = new ContextClassLoaderLocal() {   
  30.                  // Creates the default instance used when the context classloader is unavailable   
  31.                  protected Object initialValue() {   
  32.                      return new ObjectPropertyUtilsBean();   
  33.                  }   
  34.              };   
  35.     public static ObjectPropertyUtilsBean getInstance(){   
  36.         return (ObjectPropertyUtilsBean)beansByClassLoader.get();   
  37.     }   
  38.     public void copyProperties(Object dest, Object orig)   
  39.             throws IllegalAccessException, InvocationTargetException,   
  40.             NoSuchMethodException {   
  41.   
  42.         if (dest == null) {   
  43.             throw new IllegalArgumentException ("目标对象为空");   
  44.         }   
  45.         if (orig == null) {   
  46.             throw new IllegalArgumentException("没有拷贝对象");   
  47.         }   
  48.   
  49.         if (orig instanceof DynaBean) {   
  50.             DynaProperty origDescriptors[] =   
  51.                 ((DynaBean) orig).getDynaClass().getDynaProperties();   
  52.             for (int i = 0; i < origDescriptors.length; i++) {   
  53.                 String name = origDescriptors[i].getName();   
  54.                 if (dest instanceof DynaBean) {   
  55.                     if (isWriteable(dest, name)) {   
  56.                         Object value = ((DynaBean) orig).get(name);   
  57.                         ((DynaBean) dest).set(name, value);   
  58.                     }   
  59.                 } else /* if (dest是一个标准的JavaBean) */ {   
  60.                     if (isWriteable(dest, name)) {   
  61.                         Object value = ((DynaBean) orig).get(name);   
  62.                         setSimpleProperty(dest, name, value);   
  63.                     }   
  64.                 }   
  65.             }   
  66.         } else if (orig instanceof Map) {   
  67.             Iterator names = ((Map) orig).keySet().iterator();   
  68.             while (names.hasNext()) {   
  69.                 String name = (String) names.next();   
  70.                 if (dest instanceof DynaBean) {   
  71.                     if (isWriteable(dest, name)) {   
  72.                         Object value = ((Map) orig).get(name);   
  73.                         ((DynaBean) dest).set(name, value);   
  74.                     }   
  75.                 } else /* if (dest is a standard JavaBean) */ {   
  76.                     if (isWriteable(dest, name)) {   
  77.                         Object value = ((Map) orig).get(name);   
  78.                         setSimpleProperty(dest, name, value);   
  79.                     }   
  80.                 }   
  81.             }   
  82.         } else /* if (orig is a standard JavaBean) */ {   
  83.             PropertyDescriptor origDescriptors[] =   
  84.                 getPropertyDescriptors(orig);   
  85.             for (int i = 0; i < origDescriptors.length; i++) {   
  86.                 String name = origDescriptors[i].getName();   
  87.                 if (isReadable(orig, name)) {   
  88.                     if (dest instanceof DynaBean) {   
  89.                         if (isWriteable(dest, name)) {   
  90.                             Object value = getSimpleProperty(orig, name);   
  91.                             ((DynaBean) dest).set(name, value);   
  92.                         }   
  93.                     } else /* if (dest is a standard JavaBean) */ {   
  94.                         if (isWriteable(dest, name)) {   
  95.                             Object value = getSimpleProperty(orig, name);   
  96.                             setSimpleProperty(dest, name, value);   
  97.                         }   
  98.                     }   
  99.                 }   
  100.             }   
  101.         }   
  102.   
  103.     }   
  104.        
  105.        
  106.        
  107.     public void setSimpleProperty(Object bean,String name, Object value)throws IllegalAccessException, InvocationTargetException,   
  108.             NoSuchMethodException {   
  109.   
  110.         if (bean == null) {   
  111.             throw new IllegalArgumentException("对象为空");   
  112.         }   
  113.         if (name == null) {   
  114.             throw new IllegalArgumentException("属性名为空");   
  115.         }   
  116.   
  117.         // Validate the syntax of the property name   
  118.         if (name.indexOf(PropertyUtils.NESTED_DELIM) >= 0) {   
  119.             throw new IllegalArgumentException ("属性名不规范");   
  120.         } else if (name.indexOf(PropertyUtils.INDEXED_DELIM) >= 0) {   
  121.             throw new IllegalArgumentException("属性名不规范");   
  122.         } else if (name.indexOf(PropertyUtils.MAPPED_DELIM) >= 0) {   
  123.             throw new IllegalArgumentException ("属性名不规范");   
  124.         }   
  125.   
  126.         // Handle DynaBean instances specially   
  127.         if (bean instanceof DynaBean) {   
  128.             DynaProperty descriptor =((DynaBean) bean).getDynaClass().getDynaProperty(name);   
  129.             if (descriptor == null) {//不存在该属性   
  130.                 return;   
  131.             }   
  132.             ((DynaBean) bean).set(name, value);   
  133.             return;   
  134.         }   
  135.   
  136.         // Retrieve the property setter method for the specified property   
  137.         PropertyDescriptor descriptor = getPropertyDescriptor(bean, name);   
  138.         if (descriptor == null) {//不存在该属性   
  139.             return;   
  140.         }   
  141.         Method writeMethod = getWriteMethod(descriptor);   
  142.         if (writeMethod == null) {   
  143.             throw new NoSuchMethodException("属性 '" + name + "' 没有Setter方法");   
  144.         }   
  145.         Class cl = getPropertyType(bean, name);   
  146.         if(value != null)/*在这你可加上把复制对象属性转为目标类属性的代码*/  
  147.         if(!cl.getName().equals(value.getClass().getName())){   
  148.             if(cl.getName().equals(Long.class.getName())){   
  149.                 if(value.getClass().getName().equals(String.class.getName()))   
  150.                     value = Long.valueOf((String)value);   
  151.                
  152.             }   
  153.         }   
  154.                
  155.         // Call the property setter method   
  156.         Object values[] = new Object[1];   
  157.         values[0] = value;   
  158.         invokeMethod(writeMethod, bean, values);   
  159.   
  160.     }   
  161.        
  162.     private Object invokeMethod(   
  163.             Method method,    
  164.             Object bean,    
  165.             Object[] values)    
  166.                 throws  
  167.                     IllegalAccessException,   
  168.                     InvocationTargetException {   
  169.                 try {   
  170.                    
  171.                 return method.invoke(bean, values);   
  172.                    
  173.                 } catch (IllegalArgumentException e) {   
  174.                    
  175.                 log.error("方法反射失败.", e);   
  176.                 throw new IllegalArgumentException(   
  177.                     "不能反射: " + method.getDeclaringClass().getName() + "."    
  178.                     + method.getName() + " - " + e.getMessage());   
  179.                    
  180.                 }   
  181.             }   
  182.   
  183.   
  184. }   
下面还要写一个类 PropertyUtil爆露一个静态方法来使用copyPropert
java 代码
  1. package com.zhgrd.basic.util;   
  2.   
  3. import java.lang.reflect.InvocationTargetException;   
  4. import org.apache.commons.beanutils.PropertyUtils;   
  5.   
  6.   
  7. public class PropertyUtil extends PropertyUtils{   
  8.      public static void copyProperties(Object dest, Object orig) throws IllegalAccessException, InvocationTargetException,   
  9.      NoSuchMethodException {   
  10.              ObjectPropertyUtilsBean.getInstance().copyProperties(dest, orig);   
  11.     }   
  12. }   
好了现在可以使用了.
java 代码
  1.         SysUserForm tform = (SysUserForm)form;   
  2.         try{   
  3.             PropertyUtil.copyProperties(tsysUser, tform);   
  4.         }catch(Exception ex){   
  5.             logger.debug("属性拷贝异常:");   
  6.             ex.printStackTrace();   
  7. }  

仅供学习.本人不保证上面代码正确.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值