在JasperReports中,当值为NULL或为空时隐藏图像

时间:2017-03-23 10:43:54

标签: image jasper-reports

我正在使用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]”。

2 个答案:

答案 0 :(得分:3)

您可以在Jaspersoft Studio中执行此操作:

enter image description here

在jrxml中创建以下行:

<image isUsingCache="true" onErrorType="Blank">

答案 1 :(得分:3)

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