我正在使用JasperReports 6.2.2版本。我想隐藏图像,如果该值为null或为空。

下面我在jrxml中使用的代码隐藏了图像,但这对我不起作用。

<image>
    <reportElement x="33" y="0" width="88" height="45" uuid="182b40f8-b18e-417c-b9f4-096199533567">
        <printWhenExpression><![CDATA[($F{urlImage}!=null ? $F{urlImage} : "")]]></printWhenExpression>
    </reportElement>
</image>


$ F {urlImage}是字符串类型

它给出了一个错误

ERROR [btpool0-2] (JRFillSubreport.java:866) - Fill 1: exception
     [java] java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Boolean
     [java]     at net.sf.jasperreports.engine.fill.JRFillElement.evaluatePrintWhenExpression(JRFillElement.java:806)
     [java]     at net.sf.jasperreports.engine.fill.JRFillImage.evaluate(JRFillImage.java:471)
     [java]     at net.sf.jasperreports.engine.fill.JRFillElementContainer.evaluate(JRFillElementContainer.java:381)
     [java]     at net.sf.jasperreports.engine.fill.JRFillFrame.evaluate(JRFillFrame.java:159)
     [java]     at net.sf.jasperreports.engine.fill.JRFillElementContainer.evaluate(JRFillElementContainer.java:381)
     [java]     at net.sf.jasperreports.engine.fill.JRFillBand.evaluate(JRFillBand.java:500)
     [java]     at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillColumnBand(JRVerticalFiller.java:2022)
     [java]     at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillDetail(JRVerticalFiller.java:748)
     [java]     at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReportStart(JRVerticalFiller.java:255)
     [java] java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Boolean
     [java]     at net.sf.jasperreports.engine.fill.JRFillElement.evaluatePrintWhenExpression(JRFillElement.java:806)
     [java]     at net.sf.jasperreports.engine.fill.JRFillImage.evaluate(JRFillImage.java:471)
     [java]     at net.sf.jasperreports.engine.fill.JRFillElementContainer.evaluate(JRFillElementContainer.java:381)
     [java]     at net.sf.jasperreports.engine.fill.JRFillFrame.evaluate(JRFillFrame.java:159)
     [java]     at net.sf.jasperreports.engine.fill.JRFillElementContainer.evaluate(JRFillElementContainer.java:381)
     [java]     at net.sf.jasperreports.engine.fill.JRFillBand.evaluate(JRFillBand.java:500)
     [java]     at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillColumnBand(JRVerticalFiller.java:2022)
     [java]     at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillDetail(JRVerticalFiller.java:748)
     [java]     at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReportStart(JRVerticalFiller.java:255)
     [java]     at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:115)
     [java]     at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:580)
     [java]     at net.sf.jasperreports.engine.fill.BaseReportFiller.fill(BaseReportFiller.java:414)
     [java]     at net.sf.jasperreports.engine.fill.JRFillSubreport.fillSubreport(JRFillSubreport.java:736)
     [java]     at net.sf.jasperreports.engine.fill.JRSubreportRunnable.run(JRSubreportRunnable.java:59)
     [java]     at net.sf.jasperreports.engine.fill.AbstractThreadSubreportRunner.run(AbstractThreadSubreportRunner.java:221)
     [java]     at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
     [java]     at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
     [java]     at java.lang.Thread.run(Unknown Source)
     [java]     at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:115)
     [java]     at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:580)
     [java]     at net.sf.jasperreports.engine.fill.BaseReportFiller.fill(BaseReportFiller.java:414)
     [java]     at net.sf.jasperreports.engine.fill.JRFillSubreport.fillSubreport(JRFillSubreport.java:736)
     [java]     at net.sf.jasperreports.engine.fill.JRSubreportRunnable.run(JRSubreportRunnable.java:59)
     [java]     at net.sf.jasperreports.engine.fill.AbstractThreadSubreportRunner.run(AbstractThreadSubreportRunner.java:221)
     [java]     at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
     [java]     at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
     [java]     at java.lang.Thread.run(Unknown Source)


如果其值为null或为空,如何隐藏图像。在urlImage字段中包含图像的url。例如:“ [http://use.example.com/pop.png][1]”。

最佳答案

printWhenExpression应该返回Boolean,当前您正在返回String

$F{urlImage}!=null ? $F{urlImage} : ""ternary表达式,如果不为null,则返回$F{urlImage}的值,否则返回空字符串

取而代之的是,您只需要返回一个Boolean,因此您将需要这样的表达式:

<printWhenExpression>
    <![CDATA[$F{urlImage}!=null && !$F{urlImage}.isEmpty()]]>
</printWhenExpression>


不为空且不为空时打印


注意:使用旧的jasper-reports lib不会自动autobox
您需要自己创建布尔对象

new Boolean($F{urlImage}!=null && !$F{urlImage}.isEmpty())

07-24 19:27