我正在尝试将对 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()
在这两种情况下,它都会提示无法解析 hello。然而它在类路径中。我还尝试右键单击报告根并将 utils.Hello/utils
添加到 Java 导入指令中。两者似乎都没有提高类效率。
非常感谢任何建议。
最佳答案
你的正确表达可能是这样的:
<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 类)必须位于类路径中.
有关使用 srciptlet 的更多信息,您可以找到 here .
关于java - 在iReport表达式中调用Java API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13023358/