在iReport表达式中调用Java API

时间:2012-10-23 03:57:03

标签: java jasper-reports ireport

我正在尝试将对 Java 库的调用附加到 iReport 表达式中。

我尝试使用一个非常简单的库来返回一个hello world字符串。

package utils;
public class Hello {
    public static String hello()
    {
        return "Hello";
    }
}

iReport 中,我想使用此API。我将上面的库编译成jar文件。在工具中添加了位置 - >选项 - >类路径

然后尝试了以下内容:

  • 在文本字段new utils.Hello().hello()
  • 中编辑表达式
  • 创建新字段并将其类型设置为utils.Hello。然后使用表达式
  • 中的field.hello()

在这两种情况下,它都抱怨它无法解决问候。然而它在类路径中。我还尝试右键单击报告根目录并将utils.Hello/utils添加到 Java import指令中。这两个似乎都没有上升。

非常感谢任何建议。

2 个答案:

答案 0 :(得分:3)

你的正确表达可能是这样的:

<textFieldExpression><![CDATA[utils.Hello.hello()]]></textFieldExpression>

工作样本:

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport ..>
    <queryString>
        <![CDATA[SELECT DISTINCT city FROM address ORDER BY city]]>
    </queryString>
    <field name="CITY" class="java.lang.String"/>
    <detail>
        <band height="20" splitType="Stretch">
            <textField>
                <reportElement x="0" y="0" width="100" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{CITY}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="100" y="0" width="100" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[utils.Hello.hello()]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

您还可以向报告添加导入说明。在这种情况下,表达式将是:

<textFieldExpression><![CDATA[Hello.hello()]]></textFieldExpression>

工作样本:

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport ... whenNoDataType="AllSectionsNoDetail">
    <import value="utils.Hello"/>
    <title>
        <band height="41">
            <textField>
                <reportElement x="188" y="11" width="100" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[Hello.hello()]]></textFieldExpression>
            </textField>
        </band>
    </title>
</jasperReport>

注意:对于这两个示例, jar 文件(包含 utils.Hello 类)必须位于类路径中

Setting classpath in iReport

有关使用srciptlets的更多信息,您可以找到here

答案 1 :(得分:0)

你的字段类型应该是String,而不是utils.Hello