1

我有一个返回类对象的方法。对象设置类的属性并返回。
我必须遍历对象并获取对象之前设置的属性的值。

我尝试使用 for-each 循环,迭代器但未能遍历。

有人可以帮我解决这个问题。在此先感谢。

代码:

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 类:

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;
    }


}

主题是对象设置 javabean 类的属性,我必须从 UI 中获取这些值。

在 Jsp 中

<%
MessageBean consumer = msg.getMessages();

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

1 回答 1

1

由于MessagesBean似乎符合javabeans 规范,您可以只使用java.beans.Introspector它。

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);
}

这一切都在使用反射 API的幕后。


更新您的编辑表明您打算使用它在 JSP 中呈现数据。那么这并不是真正正确的方法。咬紧牙关,分别指定每个属性。这样您就可以完全控制排序。

于 2012-04-26T05:02:38.643 回答