【问题标题】:Calling Java API in the iReport expression在 iReport 表达式中调用 Java API
【发布时间】:2012-10-23 15:08:43
【问题描述】:

我正在尝试将对 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 导入指令。两者似乎都没有上课。

非常感谢任何建议。

【问题讨论】:

  • new utils.Hello().hello()?你确定语法甚至可以在 Java 程序中编译吗?

标签: java jasper-reports ireport


【解决方案1】:

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

<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 类)必须在类路径中.

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 哪个选项?我的问题是得到一个 ClassNotFoundException。所以似乎我的班级没有被找到。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多