1

我有我的 pojo 课程

掌握:

public class Master extends RecordSet{

private String empcode;
private String empname;
private String dept;

public String getEmpcode() {
    return empcode;
}
public void setEmpcode(String empcode) {
    this.empcode = empcode;
}
public String getEmpname() {
    return empname;
}
public void setEmpname(String empname) {
    this.empname = empname;
}
public String getDept() {
    return dept;
}
public void setDept(String dept) {
    this.dept = dept;
}

日期范围:

public class DateRange extends RecordSet {
    private String  ddate;

    public String getDdate() {
        return ddate;
    }

    public void setDdate(String ddate) {
        this.ddate = ddate;
    }
}

记录集:

public class RecordSet {
    private DateRange daterange;
    private Master master;  

public DateRange getDaterange() {
        return daterange;
    }
    public void setDaterange(DateRange daterange) {
        this.daterange = daterange;
    }
    public Master getMaster() {
        return master;
    }
    public void setMaster(Master master) {
        this.master = master;
    }

}

我有我的休眠代码:

 SQLQuery query = session
                    .createSQLQuery("CALL AbsentReportproc(:_fromdate,:_todate)");
            query.addEntity(Master.class);
            query.addEntity(DateRange.class);
            query.setParameter("_fromdate", "2012-12-03");
            query.setParameter("_todate", "2012-12-04");

            List<Object[]> obj = query.list();
            System.out.println(obj);
            List<RecordSet> recordset = new ArrayList<RecordSet>();
            Iterator it = obj.iterator();
            while (it.hasNext()) {
                Object[] object = (Object[]) it.next();
                for (int i = 0; i < object.length; i++) {

    RecordSet set = (RecordSet) object[i];
     recordset.add(set);
        System.out.println(set);
//Here I am able to print
//edu.model.Master@15093f1
//edu.model.DateRange@e6f7d2
//edu.model.Master@19836ed
//edu.model.DateRange@3e0ebb
//edu.model.Master@39443f
//edu.model.DateRange@e6f7d2 etc................

                }
            }

    }

我的要求是使用 JSTL 标签在 jsp 中显示两者(Master 和 DateRange)的结果

任何人都可以如何获得最终的 List 对象来迭代和显示两个 pojo 的结果

我想显示 Master Pojo 的 EmployeeName、Code、Dept 和 DateRange Pojo 的 ddate?

当我迭代 RecordSet 列表时,我无法打印值(获取空指针异常)?

4

1 回答 1

1

你可以这样做:

<c:forEach var="record" items="${recordset}">
    <c:out value="${record.master.empcode}" />
    <c:out value="${record.master.empname}" />
    <c:out value="${record.master.dept}" />
    <c:out value="${record.daterange.ddate}" />
</c:forEach>

编辑

List<Object[]> obj = query.list(); 
List<RecordSet> recordset = new ArrayList<RecordSet>(); 
for (Object[] item: obj) {
    Master master = null;
    DateRange dateRange = null;
    if (item[0] instanceof Master) {
        Master master = (Master) item[0];
        DateRange dateRange = (DateRange) item[1];
    } else if (item[0] instanceof DateRange) {
        DateRange dateRange = (DateRange) item[0];
        Master master = (Master) item[1];
    }
    RocordSet record = new RecordSet();
    record.setMaster(master);
    record.setDateRange(dateRange);
    recordset.add(record);
}

并将记录集对象返回给jsp。

希望这可以帮助。

于 2013-02-07T07:51:37.020 回答