JAKARTA-Common-BeanUtils Study (1)

xiaoxiao2021-03-06  70

Author: SonyMusic 2003.05.13 I. Overview BeanUtils first saw the package, in Struts projects, Struts as a tool to use, it is estimated more functions get stronger, they moved to the Common project in the bar. BeanUtils a total of four package: the package is mainly used for three org.apache.commons.beanutilsorg.apache.commons.beanutils.convertersorg.apache.commons.beanutils.localeorg.apache.commons.beanutils.locale.converters data Conversion, surrounding a CONVERTER interface, only one way: java.lang.object communication, Java.lang.Object value), used to convert a value to another type of object-based Object . It should be useful in some automated applications. It is not commented here, I have been interested, or I feel useful, and then study. Here is only the first package.

Second, the test used Bean before starting all the tests, I wrote a simple bean for tested, the code is as follows: package test.jakarta.commons.beanutils; / ** * @Author Sonymusic * * / public Class Month {Private Int value; private intend name; private int [] days = {11, 22, 33, 44, 55}; public month (int V, string n) {value = v; Name = n;} / ** * Returns the name. * @Return string * / public string getName () {return name;} / ** * returns the value. * @Return int * / public int getValue () {return value;} / ** * sets the Name. * @Param name the name to set * / public void setName (String name) {this.name = name;} / ** * sets the value. * @Param Value the value to set * / public void setvalue (int Value) {this.Value = value;} / ** * @see java.lang.object # toString () * / public string toString () {return value "(" Name ")";} public int [] getDays () {Return Days;} public void setdays (int [] is) {days = IS;}} 3 Example of the method // static java.util.map describe (java.lang.object bean) // This method returns all readable properties in the object, and Put the property name / property value in a map, and // A property named Class is an attribute value of Object, in fact, the class is a property of java.lang.object Month Month = New Month ( 1, "jan"); try {map map = beanutils.describe (month); set keyset = map.keyset (); for (item (iterator it = keyset.ITERATOR (); it.hasnext ();) {Object Element = (Object) iter.next (); System.out.println ("KeyClass:" Element.getClass (); getName ()); System.out.Println ("ValueClass:" map.get (element) .getclass. () .getname ()); system.out.print (Element "/ t"); system.out.print (map.get (element));

System.out.println ();}} catch (IllegalAccessException e) {e.printStackTrace ();} catch (InvocationTargetException e) {e.printStackTrace ();} catch (NoSuchMethodException e) {e.printStackTrace ();} Output is: KeyClass: java.lang.StringValueClass: java.lang.Stringvalue 1KeyClass: java.lang.StringValueClass: java.lang.Stringclass class test.jakarta.commons.beanutils.MonthKeyClass: java.lang.StringValueClass: java.lang.Stringname Jan notes that Key / Value in all MAP is String, regardless of the actual value in the object of Object. There is also the corresponding Static Void Populate (java.ientil.omap printies) is used to put the MAP you just described into an object. Look at such a code Cao Xiao Gang may remember that in order to take an unsteadic object, it has a lot of time, the difficulty is not difficult, but it is necessary to do 100% correctly, still need to pay a lot. // static java.lang.string getproperty (java.lang.object bean, java.lang.string name) Month Month = New Month (1, "jan"); try {system.out.println (beanutils.getproperty (Month) , "Value"));} catch (exception e) {E.PrintStackTrace ();} // Output is: 1 similar to getindexedproperty, getMappedProperty, getMappedProperty as an example: Month Month = New Month (1, "Jan"); try {system.out.println (beanutils.getIndexedProperty); system.out.println (beanuTils.GetIndexedProperty);} catch (EXCEPTION E) {E.PrintStackTrace ();} These two calls are the same. There is also a method in Beanutils: static void copyproperties (java.lang.object dest, java.lang.object orig) it is really useful, remember that struts are full of God, I even doubt the entire beanutils is originally because of this The needs of the method are written. It copies the properties in the object orig to DEST. Fourth, PropertyUtils This class and beanutils class have many methods are the same in parameters, but the return value is different. BeanUTILS focuses on "bean", and the return value is usually string, and PropertyUtils focuses on attributes, and its return value is usually Object.

转载请注明原文地址:https://www.9cbs.com/read-87396.html

New Post(0)