在iReport表达式中调用Java API [英] Calling Java API in the iReport expression

查看:73
本文介绍了在iReport表达式中调用Java API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to attach a call to a Java library into the iReport expression.

我用一个非常简单的库尝试了此操作,该库返回了hello world字符串.

I tried this with a very simple library returning a hello world string.

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

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

Within iReport, i want to use this API. I compiled the above library into a jar file. Added the location in Tools -> Options -> Classpath.

然后尝试以下操作:

  • 在文本字段中编辑表达式 new utils.Hello().hello()
  • 创建一个新字段并将其类型设置为 utils.Hello .然后在表达式中使用 field.hello()
  • Editing the expression in a a text field new utils.Hello().hello()
  • Creating a new field and setting its type to utils.Hello. Then using the field.hello() in the expression

在两种情况下,它都抱怨无法解决问候.但是,它在类路径中.我还尝试右键单击报表根目录,然后将 utils.Hello/utils 添加到 Java 导入指令中.似乎都没有上课.

In both cases, it complains that it cannot resolve hello. However its in the classpath. I've also tried to right click on the report root and add utils.Hello/utils to the Java import directive. Neither of which seemed to pick up the class.

任何建议都将不胜感激.

Any advice is greatly appreciated.

推荐答案

您正确的表达方式可能是这样的:

Your right expression might be like this:

<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>

您还可以将导入说明添加到报告中.在这种情况下,表达式将为:

You can also add import instruction to the report. In this case the expression will be:

<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 类)都必须位于类路径中

Note: For both samples the jar file (with utils.Hello class) must be in classpath.

有关使用srciptlet的详细信息,您可以在此处找到

More info about using srciptlets you can find here.

这篇关于在iReport表达式中调用Java API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆