【问题标题】:How to access private and protected JRElements members of JasperDesign object?如何访问 JasperDesign 对象的私有和受保护的 JRElements 成员?
【发布时间】:2017-06-20 19:58:03
【问题描述】:

我正在使用 JasperDesign 类创建 Jasper 对象,并使用 jrxml 文件中的 JRXmlLoader 启动它。 我正在使用 getAllBands() 方法提取所有 JRBand 以获取所有 JRBand,并且从每个频段中,我正在使用 JRBand 的 getElements() 方法提取 JRElements。

但是,在获取 staticFieldtextField 之类的每个元素后,我无法从私有或受保护的“TEXT”字段中获取它们的值。

如何访问这些值?

【问题讨论】:

  • 你应该发布代码 - 重现问题
  • @AlexK 这不是问题。 JRElements 类的文本值没有 getter。由于 Jasper 库是一个开源库,我可以创建自己的 getter 来获取它吗?如果是,那怎么办?
  • Since Jasper library is an open source library, Could I create my own getter to get it? - 是的。你可以做你想做的一切。是Java
  • @AlexK 问题是怎么回事?库太大,无法进行任何更改。
  • 不需要对库做任何更改,我想我已经理解了你的问题,你只需要将JRElement 转换为相对JRDesign 类,看我的回答。跨度>

标签: java jasper-reports


【解决方案1】:

您需要将JRElement 强制转换为相关的 JRDesign 类才能访问元素的特定属性:

示例

jrxml

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="testJasperDesign" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="597c0716-df6b-42ec-a7c8-863eb1b7174a">
    <parameter name="testParam" class="java.lang.String">
        <defaultValueExpression><![CDATA["Hello world"]]></defaultValueExpression>
    </parameter>
    <variable name="variable1" class="java.lang.String"/>
    <title>
        <band height="32" splitType="Stretch">
            <textField>
                <reportElement x="100" y="0" width="100" height="20" uuid="bf5a8f35-3faf-457b-a6fc-b29d97a9c332"/>
                <textFieldExpression><![CDATA[$P{testParam}]]></textFieldExpression>
            </textField>
            <staticText>
                <reportElement x="0" y="0" width="100" height="20" uuid="59922664-93a3-4f69-a906-5ff418d09cd3"/>
                <text><![CDATA[Static text]]></text>
            </staticText>
        </band>
    </title>
</jasperReport>

Java

public static void main(String[] args) throws JRException {

    JasperDesign design = JRXmlLoader.load("jasper/testJasperDesign.jrxml");
    JRBand titleBand = design.getTitle();
    JRElement[] elements = titleBand.getElements();
    for (JRElement element : elements) {
        if (element instanceof JRDesignTextField){
            JRDesignTextField textField = (JRDesignTextField) element;
            JRExpression expression = textField.getExpression();
            System.out.println(expression.getText());
        }
        if (element instanceof JRDesignStaticText){
            JRDesignStaticText staticText = (JRDesignStaticText) element;
            System.out.println(staticText.getText());
        }

    }
}

输出

$P{testParam}
静态文本

【讨论】:

  • 完美答案。它为我工作。非常感谢。
  • 您好 Petter,调用 jasperfillmanager 的 fillReport() 方法后,从哪里获取文本字段的表达式值(评估后)。我用 java bean 源填充它。
  • @SaurabhSonkar 对不起,我无法真正理解这个问题,JasperDesign 在您填写之前(因此没有来自数据源的值),在您填写之后您有 JasperPrint(没有更多的乐队),但从这里你可以得到最终的结果文本。
  • 这篇文章展示了如何迭代 JasperPrint stackoverflow.com/a/42426312/5292302,它会找到某个 JRPrintText 并对其进行更改
  • 您说得对,但我没有使用页面。我的所有内容都在一个带中​​,如标题、摘要、详细信息 1、详细信息 2 等。我可以遍历这个 JasperPrint 对象吗?
猜你喜欢
  • 2016-07-15
  • 2012-05-03
  • 2016-05-22
  • 2013-12-30
  • 2014-04-24
  • 2018-01-18
  • 2023-03-03
  • 2017-08-01
  • 2016-10-01
相关资源
最近更新 更多