遍历java中的对象 [英] Travesring through an object in java

查看:239
本文介绍了遍历java中的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

有人可以帮我解决这个问题吗?提前致谢.

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

代码:

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

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


}

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

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

在 Jsp 中

<%
MessageBean consumer = msg.getMessages();

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

推荐答案

由于 MessagesBean 似乎符合 javabeans 规范,你可以使用 java.beans.Introspector 为此.

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

这一切都在使用 reflection API 的幕后.

This all is under the covers using the reflection API.

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

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.

这篇关于遍历java中的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆