How do I read bean's property value?

Category: commons.beanutils, viewed: 4270 time(s).

In this example we'll learn how to use PropertyUtils.getSimpleProperty() to read a value of bean property. We'll create a Track class and this will be our bean. The PropertyUtils.getSimpleProperty() reads our bean property by accessing the bean getter methods. In this example we'll read the track title.

package org.kodejava.example.commons.beanutils;

import java.lang.reflect.InvocationTargetException;
import org.apache.commons.beanutils.PropertyUtils;

public class ReadBeanProperty {
    public static void main(String[] args) {
        Track track = new Track();
        track.setTitle("Till There Was You");
        
        try {
            String title = (String) PropertyUtils.getSimpleProperty(track, "title");
            System.out.println("Title = " + title);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
    }    
}

package org.kodejava.example.commons.lang;

public class Track {
    private Integer id;
    private String title;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }                
}
Click here to lend your support to: Kode Java Org and make a donation at www.pledgie.com !

 

Uncensored Newsgroups
Download Hundreds of Complimentary Industry Resources

Get hundreds of popular Industry magazines, white papers, webinars, podcasts, and more; all available at no cost to you. With more than 600 complimentary offers, you'll find plenty of titles to suit your professional interests and needs. Click Here and Sign up today!

Java Training

Sponsored Links

Our Friends

Statistics

Locations of visitors to this page
visitor stats