Apache Jakarta and Beyond: A Java Programmeramp;#039;s Introduction [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Apache Jakarta and Beyond: A Java Programmeramp;#039;s Introduction [Electronic resources] - نسخه متنی

Larne Pekowsky

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید







8.6. Converters


All the calls to the set methods of PropertyUtils in the preceding sections passed in objects that matched the type expected by the underlying set method in the bean. For example, when setting album.name a string was provided, and when setting artist.orderedAlbums[0] an instance of Album was given. The situation is somewhat more complicated when setting something that is not an object, such as the year an album was released. The temptation would be to call setSimpleProperty() with an int as the second argument, but the method only accepts objects. This means that the int would normally need to be boxed in an Integer


PropertyUtils.setSimpleProperty(album, releaseYear,
new Integer(2003));

The low-level reflection API automatically handles any needed conversion from objects to primitive types when setting properties and from primitive types to objects when getting properties.

BeanUtils takes this idea several steps further by introducing

converters. Converters are classes that can automatically convert from one type to another based on the type that is needed and the type that is provided. In particular this makes it possible to set int and Integer values from a String, as in

PropertyUtils.setSimpleProperty(album,releaseYear,"2003");

Converters are also available that convert to Double, Boolean, and so on. There are even converters that can translate from strings to arrays. An array may be represented as a sequence of elements separated by commas or white space and for clarity may also be delineated by bracesfor example, "{1,8,45,2}" could be used as the value for a property comprising an array of integers.

The ability to use strings without worrying about the underlying type is very handy because most user input arrives in the form of a string. This is true for applications that run from the command line, GUIs, the Web, and so on.


/ 207