JAKARTA-Common-BeanUtils study notes

xiaoxiao2021-03-06  38

JAKARTA-Common-BeanUtils uses notes

1 Overview

The first contact beanUtils is in the process of learning Struts, in Struts it is largely used to process the FORMBEAN.

BeanUtils mainly provides various operations for JavaBean.

Beanutils is divided into 4 packs:

Ø Org.apache.commons.beanutils

Ø Org.apache.commons.beanutils.converters

Ø Org.apache.commons.beanutils.locale

Ø Org.apache.commons.beanutils.locale.converters

The above two are the default implementation of BeanUtils, which has no processing of localization, which can improve execution efficiency. But if your procedure is required for localization, then the following 2 packages are more secure.

2. Org.apache.commons.beanutils

This package mainly provides tool classes for operating JavaBean, and the main features of Jakarta-Common-Beanutils are implemented in this package.

The following describes several major tools:

2.1. Beanutil

1. First, I first define a JavaBean as the operation object of the later example.

Public Class Company

{

PRIVATE STRING NAME;

Private hashmap address = new hashmap ();

Private string [] OtherInfo;

Private arraylist products;

PRIVATERAYLIST EMPLOYEE

Private hashmap telephone;

Public company () {}

Public string getName ()

{

Return Name;

}

Public void setname (String name)

{

THIS.NAME = Name;

}

Public String getaddress (String Type)

{

Return Address.get (Type) .tostring ();

}

Public void setaddress (String Type, String Address)

{

this.address.put (Type, Address);

}

Public String [] getotherinfo ()

{

Return OtherInfo;

}

Public void setotherinfo (String [] OtherInfo)

{

THIS. Secherinfo = OtherInfo;

}

Public ArrayList getProduct ()

{

Return Product;

}

Public void setProduct (ArrayList Product)

{

THIS.PRODUCT = Product;

}

Public arraylist geteMPloyee ()

{

Return Employee;

}

Public void setEmployee (arraylist employee)

{

this.employee = Employee;

}

Public hashmap gettelephone ()

{

Return televor

}

Public void settelephone (havehmap telephone)

{

This.Telephone = Televhone;

}

}

2, beanutils can directly get the value of a attribute of GET and SET. It divides Property into three types: Simple - simple type, such as stirng, int ...

Indexed - index type, such as array, arraylist ...

Maped - this doesn't have to say it, it should be, it means Map, such as Hashmap ...

Accessing different types of data can directly call functions getProperty and setProperty. They all have only 2 parameters, the first is the JavaBean object, the second is the attribute name to operate.

Company c = new company ();

C.setname ("Simple");

For Simple type, the parameter 2 is directly attribute nominal.

// simple

System.out.println (beanutils.getProperty);

For the MAP type, you need to "property name (key value)" form.

// map

System.out.println (beanUtils.getProperty)))))));

Hashmap am = new hashmap ();

AM.PUT ("1", "234-222-1222211");

Am.put ("2", "021-086-1232323");

Beanutils.SetProperty (C, "Telephone", AM);

System.out.println (beanutils.getProperty))))))

For indexed, "Property Name [index value]", note that it can be operated in the same way for ArrayList and arrays.

// index

System.out.println (beanutils.getProperty (C, "OtherInfo [2])))))

Beanutils.SetProperty (C, "Product [1]", "Notes Server");

System.out.println (beanutils.getProperty))));

Of course, these three types can also be used in combination!

// Nest

System.out.println (beanUtils.getProperty (C, "Employee [1] .name"));

3, in addition, there is a very important method CopyProperty that can directly carry out the Clone between Beans.

Company C2 = New Company ();

Beanutils.copyProperties (C2, C);

However, this Copy is shallow copy. The same attribute of 2 beans after copying may have the same object of REF, which is careful when using, especially for the attribute.

4, in the end, there is Populate, which is used to fill the value of a MAP into a bean, its function prototype is as follows:

Public void populate (java.lang.Object bean,

Java.util.map Properties)

Throws java.lang.illegalaccessException,

Java.Lang.Reflect.InvocationTargeTexception

In Struts, this function is used to add parameters from HTTP Request, which seems to have not seen this function. What other uses? ! I think later: p2.2. Lazydynabean

It implements a dynamic bean, you can join the property directly, use as a JavaBean, you can also operate with the above BeanUtils or Get / SET method without exporting a standard JavaBean class :)

Remember to have a value of Value Object in "J2EE Design Mode", used to pass data between the MVC layers, avoid direct transmission of large business objects, in order to avoid a lot of bean classes in the project, in the book Provides an implementation of dynamic value object (by extending MAP). Here LazydyNabean can be used as a more mature, stable implementation: P

The words retired, Lazybean did provide a very good Dynabean implementation, using almost no excess code ^ _ ^, let's take a look at the example!

// Use lazydynamap here, it is a lightweight implementation of Lazybean

LazydynaMap Dynabean1 = new lazydynamap ();

Dynabean1.set ("foo", "bar"); // simple

Dynabean1.set ("Customer", "Title", "MR"); // mapped

Dynabean1.set ("Address", 0, "Address1"); // Indexed

System.out.println (Dynabean1.Get ("Address", 0));

Map mymap = dynabean1.getmap (); // Retrieve the Map

System.out.println (MyMap.Tostring ());

The above example can be seen that it can automatically increase the preordy of the bean when SET (both genuine the property to increase the property), and also supports the 3 typing property, and LazydyNAMap can also be exported to Map.

There are two important fields for this class to pay attention:

ReturnnUll - specifies that the behavior of Dynabean when using an unselected Property in the GET method.

// Information of the field

DynaBean1.setReturnNun (TRUE); // is set to TURE. Returns NULL if this field is not in the bean

// The default is false. If there is no such field in the bean, it will be automatically added :)

System.out.println (Dynabean1.get ("aaa")); // Back to NULL

Restricted - Specifies whether to allow this bean's Property.

//MutableDynaclass.setRestricted is set to TRUE, the field can no longer add and modify it.

// Default is false, allowing to add and modify

Dynabean1.setrestricted (TRUE);

Dynabean1.set ("test", "error"); / / here will be wrong!

By setting two properties, you can prevent accidental modification of the Dynabean's Property. When designing the architecture, you can automatically generate Dynabean from the data table or XML file in the background, setting the above properties before passing to the control layer and the representation layer, so that the bean structure is not allowed to modify, so it is impossible to modify the properties contained in the bean inadvertently. ...... This can be enjoyed with its convenience, but also prevent the incorrect from this! ! 3. Others

3.1. BeanUtils and PropertyUtils

These two classes have almost a touch of functions, the only difference is that beanutils are converted to the bean assignment. For example, when CopyProperty is the same as the attribute name, in additional type, beanutils can also make COPY; and PropertyBean may report an error! !

For the above example, create a new COMPANY2 class, where the code is the same as Company, just change OtherInfo to String.

Company c = init ();

Company

2 c2 = new company2 ();

Beanutils.copyProperties (C2, C);

// PropertyUtils.copyProperties (C2, C); this sentence will report an error! !

System.out.println (C2.GetotherInfo ());

Of course, the type of attribute between the two beans must be transformed, otherwise it will be an error like BeanUTils.

If you implement the org.apache.commons.beanutils.converter interface, you can customize the transformation between the type.

Because of the type conversion, it will have great improvement with PropertyUtils!

In addition, there is an advantage that the type of transformation is, as follows:

// Test Data Type Convert

// arraylist a1 = bean@utils.getProperty (c, "product"); // beanutils returns String

System.out.println ("-" beanutils.getProperty (c, "product"))); // is directly converted to String

ArrayList a = (arraylist) PropertyUtils.getProperty (C, "Product"); // PropertyUtils Returns Object

System.out.println ("-" a.get (1));

With beanutils, you cannot return an object (unless you write a Converter), it will automatically turn the type, and then return String. If you want to return to a Java class or a custom class, don't be old.

3.2. UTILS class

All XXXUTILS classes provide static methods, can be called directly, and its main implementation is in the corresponding XXXUTILSBEAN:

Beanutils -> BeanUtilsbean

ConvertUtils -> ConvertUTILSBean

PropertyUtils -> PropertyUtilsbean

It should also know how much the name is similar, I will no longer talk nonsense! Of course, you can also call those XXXUTILSBEAN directly, the function is the same!

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

New Post(0)