Beanutils uses magic reflection technology to implement a lot of exaggerated and useful functions, which are hard to imagine in the C/C ++ era. No matter who the project is, it will always be used in a day. I have never realized it. I missed it the first time I saw it.
1. Dynamic getter and setter of attributes
In the age when the framework was full, the getter and setter functions were not always executed. Sometimes the attributes were obtained dynamically based on the name, just like this:
BeanUtils.getProperty(myBean,"code");
The better feature of common beanutils is that it can directly access the attributes of embedded objects, as long as they are separated by dots.
BeanUtils.getProperty(orderBean, "address.city");
In contrast, beanutils of other class libraries is usually very simple and cannot access embedded objects. Therefore, you may need to replace them with commons beanutils.
Beanutils also supports list and map attributes. The following syntax can be used to obtain the name of the first customer in the order customer list.
BeanUtils.getProperty(orderBean, "customers[1].name");
Beanutils converts the string type to the real type of the bean attribute using the convertutils class, facilitating bean extraction from objects such as httpservletrequest, or outputting the bean to the page. Propertyutils retains the original bean type.
2. beancompartor dynamic sorting
Or through reflection, we can dynamically set the attribute to which the bean is sorted, instead of implementing the bean compare interface for Complex Condition judgment.
List tables les =...; // list of person objects collections. Sort (Tables les, new beancomparator ("Age "));
If you want to support composite sorting of multiple attributes, such as "order by lastname, firstname"
ArrayList sortFields = new ArrayList();sortFields.add(new BeanComparator("lastName"));sortFields.add(new BeanComparator("firstName"));ComparatorChain multiSort = new ComparatorChain(sortFields);Collections.sort(rows,multiSort);
Comparatorchain belongs to the jakata commons-collections package.
If the age attribute is not of the normal type, the constructor needs to input a comparator object to sort the age variable.
In addition, beancompartor's compareblecomparator throws an exception when its attribute is null, and cannot set the ascending or descending order. At this time, we need to use the comparatorutils of the commons-collections package.
Comparator mycmp = comparablecomparator. getinstance ();
Mycmp = comparatorutils. nulllowcomparator (mycmp); // allow null
Mycmp = comparatorutils. reversedcomparator (mycmp); // reverse
Comparator CMP = new beancomparator (sortcolumn, mycmp );
3. converter binds the string in the request or resultset to the attributes of the object.
It is often necessary to retrieve values from objects such as request and resultset to be assigned to the bean. If the binding function of the MVC framework is not used, no one of the following code will get bored.
String a = request.getParameter("a"); bean.setA(a); String b = ....
bean.setB(b);
......
Write a binder to automatically bind all attributes:
MyBean bean = ...; HashMap map = new HashMap(); Enumeration names = request.getParameterNames(); while (names.hasMoreElements()) { String name = (String) names.nextElement(); map.put(name, request.getParameterValues(name)); } BeanUtils.populate(bean, map);
The beanutils populate method or getproperty and setproperty Methods call convert for conversion.
Converter only supports some basic types, and even the java. util. date type is not supported. In addition, it is stupid because an exception is thrown when you encounter a unknown type. For the date type, I have implemented a converter according to its sqldate type, and added a function to set the date format.
To register this converter, you need the following statement:
Convertutilsbean convertutils = new convertutilsbean ();
Dateconverter = new dateconverter ();
Convertutils. Register (dateconverter, date. Class );
// To register converter, you cannot use the beanutils static method. You must create a beanutilsbean instance.
Beanutilsbean beanutils = new beanutilsbean (convertutils, new propertyutilsbean ());
Beanutils. setproperty (bean, name, value );
4. Other functions
4.1 constructorutils: dynamically create objects
public static Object invokeConstructor(Class klass, Object arg)
4.2 methodutils, dynamic call Method
MethodUtils.invokeMethod(bean, methodName, parameter);
4.3 propertyutils: When the attribute is collection and map, it dynamically reads:
Collection: Provides Index
BeanUtils.getIndexedProperty(orderBean,"items",1);
Or
BeanUtils.getIndexedProperty(orderBean,"items[1]");
Map: provides key value
BeanUtils.getMappedProperty(orderBean, "items","111");//key-value goods_no=111
Or
BeanUtils.getMappedProperty(orderBean, "items(111)")
4.4 propertyutils: directly obtains the class type of the attribute.
public static Class getPropertyType(Object bean, String name)
4.5 dynamic BeanSee remove unnecessary vo and formbean trackback with dynabean: http://tb.blog.csdn.net/TrackBack.aspx? Postid = 277748