表工具显示一个较少/较少的数据库记录

时间:2013-09-13 13:34:43

标签: jasper-reports

我正在使用 JasperReports 。在那我使用表和java bean数据源。但是当我将数据从我的java类发送到jrxml表时,它会显示少一条记录。例如,如果我的java类中有四条记录,但在将其导出为报告时,它只显示三条记录。 数据Bean如下: 公共类DataBean实现Serializable {

private static final long serialVersionUID = 1L;

private String description;
private String location;
private Date date;
private String type;
private String state;
private String comments;

/**
 * Parameterized constructor.
 * 
 * @param description
 *            description of alarm.
 * @param location
 *            location of alarm.
 * @param date
 *            date of alarm.
 * @param type
 *            type of alarm.
 * @param state
 *            state of alarm.
 * @param note
 *            note/comments of alarm.
 */
public DataBean(String description, String location, Date date,
        String type, String state, String note) {
    this.description = description;
    this.location = location;
    this.date = date;
    this.type = type;
    this.state = state;
    this.comments = note;
}

/**
 * Getter method to get the
 * 
 * @return location
 */
public String getLocation() {
    return location;
}

/**
 * Setter method to set the location.
 * 
 * @param location
 */
public void setLocation(String location) {
    this.location = location;
}

/**
 * Getter method to get the date.
 * 
 * @return date
 */
public Date getDate() {
    return date;
}

/**
 * Setter method to set the date.
 * 
 * @param date
 */
public void setDate(Date date) {
    this.date = date;
}

/**
 * Getter method to get the type.
 * 
 * @return type
 */
public String getType() {
    return type;
}

/**
 * Setter method to set the type.
 * 
 * @param type
 */
public void setType(String type) {
    this.type = type;
}

/**
 * Getter method to get the state.
 * 
 * @return state
 */
public String getState() {
    return state;
}

/**
 * Setter method to set the state.
 * 
 * @param state
 */
public void setState(String state) {
    this.state = state;
}

/**
 * Getter method to get the description.
 * 
 * @return description
 */
public String getDescription() {
    return description;
}

/**
 * Setter method to set the description.
 * 
 * @param description
 */
public void setDescription(String description) {
    this.description = description;
}

/**
 * Getter method to get the comments.
 * 
 * @return comments
 */
public String getComments() {
    return comments;
}

/**
 * Setter method to set the comments.
 * 
 * @param comments
 */
public void setComments(String comments) {
    this.comments = comments;
}

}

我的主要课程如下: 列出beanCollection = new ArrayList();

    for (int i = 0; i < 2; i++) {
        DataBean bean = new DataBean("Alarm Description", "Location",
                new Date(), "EventScheduleStopped-12", "AlarmAcknowledged",
                "This is the test note for the alarm.");
        beanCollection.add(bean);
    }
    System.out.println(beanCollection.size());
    List<FilterBean> filterCollection = new ArrayList<FilterBean>();
    FilterBean filterBean = new FilterBean("Last 7 Days", "Any Type",
            "Open");
    filterCollection.add(filterBean);

    InputStream inputStream = ReportManagementController.class
            .getResourceAsStream("/report.jrxml");

    JRBeanCollectionDataSource beanColDataSource = new JRBeanCollectionDataSource(
            beanCollection);

    JRBeanCollectionDataSource filterBeanCollectionDataSource = new JRBeanCollectionDataSource(
            filterCollection);

    Map parameters = new HashMap();
    parameters.put(SHOW_COMMENTS, true);
    parameters.put(SHOW_FILTERS, false);
    parameters.put("logo",
            ReportManagementController.class.getResource(NEW_IMAGE_NAME)
                    .getPath());
    parameters.put("TableDataSource", beanColDataSource);
    parameters.put("filterDataSource", filterBeanCollectionDataSource);
    JasperDesign jasperDesign = JRXmlLoader.load(inputStream);
    JasperReport jasperReport = JasperCompileManager
            .compileReport(jasperDesign);
    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,
            parameters, beanColDataSource);

    JasperViewer.viewReport(jasperPrint);

和我的jrxml(只显示jrxml的部分内容,因为它太大了)如下:

parameter name =“TableDataSource”class =“net.sf.jasperreports.engine.JRDataSource”/

dataSourceExpression&GT;![CDATA [$ P {TableDataSource}]]&GT; dataSourceExpression&GT; 有人帮帮我。

1 个答案:

答案 0 :(得分:4)

这是JR的已知特征(或错误?)。

要解决此问题,请将集合(不是JRDataSource!)作为参数传递,并在dataSource中使用此集合作为表。

例如:

parameters.put("beanCollection", beanCollection);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,
            parameters, new JREmptyDataSource());

在jrxml中定义参数“beanCollection”as java.util.Collection

并将jrxml中的表的DataSource表达式定义为

new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{beanCollection})