2009-03-05 4 views
1

BeanUtils copyProperties, out of the box, scheint nicht mit dem Kopieren von booleschen Objekteigenschaften in boolesche primitive Eigenschaften umzugehen.Wie kann ich BeanUtils copyProperties verwenden, um von boolesch zu boolesch zu kopieren?

Ich dachte, ich könnte einen Konverter erstellen und registrieren, aber das schien einfach nicht zu funktionieren.

Also, wie kann ich BeanUtils verwenden, um die Eigenschaften von Klasse Quelle Klasse Destination zu kopieren, in dem:

public class Destination { 

    private boolean property; 

    public boolean isProperty() { 
     return property; 
    } 

    public void setProperty(boolean property) { 
     this.property = property; 
    } 
} 


public class Source{ 

    private Boolean property; 

    public Boolean getProperty() { 
     return property; 
    } 

    public void setProperty(Boolean property) { 
     this.property = property; 
    } 
} 
+0

I bin jetzt auch auf diesem hier festgefahren. Plz fügen Sie eine Antwort "Träger", wenn Sie in der Lage waren, es zu tun :) –

Antwort

0

Es ist eigentlich umgekehrt:

public static void main(String[] args) throws Exception { 
    Source d = new Source(); 
    d.setProperty(Boolean.TRUE); 
    BeanMap beanMap = new BeanMap(d); 

    Destination s = new Destination(); 
    BeanUtils.populate(s, beanMap); 
    System.out.println("s.getProperty()=" + s.isProperty()); 
} 
0
public class Destination { 
    private boolean property; 

    // code getProperty() instead 
    public boolean isProperty() { 
     return property; 
    } 

    public void setProperty(boolean property) { 
     this.property = property; 
    } 
} 
1
try creating both 
/*by default beanutils copyproperties looks for below method if you use either apache or spring flavour of beanutils. 
always prefer using apache 1.9.2 (fixed many bugs) but bit slow compared with spring beanutils.*/ 
public Boolean getProperty() { 
     return property; 
    } 
//which is used by some frameworks 
public Boolean isProperty() { 
     return property; 
    } 
+0

müssen Konverter verwenden, wenn wir Apache Beanutils verwenden, als ob wir haben ConvertUtils.register (new DateConverter (null), java.util.Date.class); – RamPrakash

Verwandte Themen