Travesring through an object in java

一世执手 提交于 2019-12-13 02:57:12

问题


I have a method that return an object of a class.The object sets the properties of class and returns.
I have to traverse the object and get the value of the properties which the object has set before.

I tried to use for-each loop,iterator but failed to traverse.

Can someone please help me to get through this.Thanks in advance.

code:

public class ConsumerTool {

 public MessageBean getMessages() {
        MessageBean msgBean = new MessageBean();

        msgBean.setAtmId(atmId.trim());
        msgBean.setEventText(eventText.trim());
        msgBean.setEventNumber(eventNumber.trim());
        msgBean.setSeverity(severity.trim());
        msgBean.setSubsystemID(subsystemID.trim());
        msgBean.setUniqueEventID(uniqueEventID.trim());
        msgBean.setTaskID(taskID.trim());
        msgBean.setGenerator(generator.trim());
        msgBean.setGeneratorBuildVsn(generatorBuildVsn.trim());
        msgBean.setDateTime(dateTime.trim());

        this.msgBean = msgBean;
        return msgBean;
    }
}

JavaBean class:

public class MessageBean implements java.io.Serializable {  

    public String dateTime;
    public String severity;
    public String eventText;
    public String eventNumber;
    public String generator;
    public String generatorBuildVsn;
    public String atmId;
    public String uniqueEventID;
    public String subsystemID;
    public String taskID;

    //System.out.println("dateTime2222222"+dateTime);

    public String getAtmId() {
        return this.atmId;
    }

    public void setAtmId(String n) {
        this.atmId = n;
    }

    public String getDateTime() {
        return this.dateTime;
    }

    public void setDateTime(String n) {
        this.dateTime = n.trim();
    }

    public String getEventNumber() {
        return this.eventNumber;
    }

    public void setEventNumber(String n) {
        this.eventNumber = n;
    }

    public String getEventText() {
        return this.eventText;
    }

    public void setEventText(String n) {
        this.eventText = n;
    }

    public String getGenerator() {
        return this.generator;
    }

    public void setGenerator(String n) {
        this.generator = n;
    }

    public String getGeneratorBuildVsn() {
        return this.generatorBuildVsn;
    }

    public void setGeneratorBuildVsn(String n) {
        this.generatorBuildVsn = n;
    }

    public String getSeverity() {
        return this.severity;
    }

    public void setSeverity(String n) {
        this.severity = n;
    }

    public String getSubsystemID() {
        return this.subsystemID;
    }

    public void setSubsystemID(String n) {
        this.subsystemID = n;
    }

    public String getTaskID() {
        return this.taskID;
    }

    public void setTaskID(String n) {
        this.taskID = n;
    }

    public String getUniqueEventID() {
        return this.uniqueEventID;
    }

    public void setUniqueEventID(String n) {
        this.uniqueEventID = n;
    }


}

The theme is the object sets the properties of javabean class and I have to get those values from UI.

In Jsp

<%
MessageBean consumer = msg.getMessages();

//Now here i want to iterate that consumer object
%>

回答1:


As the MessagesBean seems to comply the javabeans specification, you can just use java.beans.Introspector for this.

MessageBean messageBean = consumerTool.getMessages();
// ...

BeanInfo beanInfo = Introspector.getBeanInfo(MessageBean.class);

for (PropertyDescriptor property : beanInfo.getPropertyDescriptors()) {
    String name = property.getName();
    Object value = property.getReadMethod().invoke(messageBean);
    System.out.println(name + "=" + value);
}

This all is under the covers using the reflection API.


Update your edit reveals that you're intending to use this to present the data in JSP. This is then not really the right approach. Bite the bullet and specify every property separately. This way you've full control over the ordering.



来源:https://stackoverflow.com/questions/10327411/travesring-through-an-object-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!