iReport中的if语句

在iReport中可以用if语句来判断组件是否显示,使用的是printWhenExpression属性,属性值的格式是这样的:

new Boolean(xxxxxxx)

括号里是返回boolean值的表达式


举个例子:

我定义了一个Label,没有内容文字,只是一个红框框,如果后台传来的某参数符合某个值,就显示这个框框,否则不显示,在Designer视图和XML视图下分别是这么配置的:

Designer视图:


printWhenExpression属性里设置的值可以是:

new Boolean($F{packageCount}=="121包")

或者 

new Boolean($F{packageCount}==null)

packageCount是我自己定义的变量,从后台传过来的,如果这么写,代表如果packageCount的值是121包的时候,这个Label会显示,否则不会显示。

 

XML视图配置如下:

                            <staticText>
                                     <reportElementx="348" y="50" width="39" height="22"forecolor="#FF3333"uuid="48701fb2-f14f-4eb0-8f66-0d6b969a4488">
                                               <printWhenExpression><![CDATA[newBoolean($F{packageCount}=="121包")]]></printWhenExpression>
                                     </reportElement>
                                     <box>
                                               <topPenlineWidth="2.0"/>
                                               <leftPenlineWidth="2.0"/>
                                               <bottomPenlineWidth="2.0"/>
                                               <rightPenlineWidth="2.0"/>
                                     </box>
                                     <textElementtextAlignment="Center" verticalAlignment="Middle">
                                               <fontsize="14" isBold="true"pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H"isPdfEmbedded="true"/>
                                     </textElement>
                                     <text><![CDATA[]]></text>
                            </staticText>

重点是那个<printWhenExpression>,其他的都是设置Label的其他属性用的

除了Label之外,其他的组件也可以使用printWhenExpression属性。


另外我试了一下这个表达式的格式,有以下结论:

  • new Boolean($F{packageCount}=="121包") 可以
  • new Boolean($F{packageCount}=='121包') 可以
  • new Boolean($F{packageCount}==121包) 不行,过不了编译
  • new Boolean($F{packageCount}!=121) 可以
  • new Boolean($F{packageCount}.intValue()!=121) 可以通过编译,但是如果后台传来的参数不是数字的话,会报如下异常:

net.sf.jasperreports.engine.fill.JRExpressionEvalException:Error evaluating expression :
         Sourcetext : new Boolean($F{packageCount}.intValue()!=121)
         atnet.sf.jasperreports.engine.fill.JREvaluator.evaluate(JREvaluator.java:263)
         atnet.sf.jasperreports.engine.fill.JRCalculator.evaluate(JRCalculator.java:611)
         atnet.sf.jasperreports.engine.fill.JRCalculator.evaluate(JRCalculator.java:579)
         atnet.sf.jasperreports.engine.fill.JRFillElement.evaluateExpression(JRFillElement.java:1016)
         atnet.sf.jasperreports.engine.fill.JRFillElement.evaluatePrintWhenExpression(JRFillElement.java:795)
         atnet.sf.jasperreports.engine.fill.JRFillStaticText.evaluate(JRFillStaticText.java:113)
         atnet.sf.jasperreports.engine.fill.JRFillElementContainer.evaluate(JRFillElementContainer.java:259)
         atnet.sf.jasperreports.engine.fill.JRFillBand.evaluate(JRFillBand.java:456)
         atnet.sf.jasperreports.engine.fill.JRVerticalFiller.fillBandNoOverflow(JRVerticalFiller.java:467)
         atnet.sf.jasperreports.engine.fill.JRVerticalFiller.fillPageHeader(JRVerticalFiller.java:431)
         atnet.sf.jasperreports.engine.fill.JRVerticalFiller.fillReportStart(JRVerticalFiller.java:292)
         atnet.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:152)
         atnet.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:963)
         atnet.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:892)
         atnet.sf.jasperreports.engine.fill.JRFiller.fill(JRFiller.java:114)
         atnet.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:668)
         atnet.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:984)
         atcom.dangdang.webx.tms.util.IReportUtils.outPdf(IReportUtils.java:56)
         atcom.dangdang.webx.tms.sort.web.action.SortDeliveryHandoverSumController.sumDeliveryPrintAgain(SortDeliveryHandoverSumController.java:1844)
         atsun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
         atsun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
         atsun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
         atjava.lang.reflect.Method.invoke(Method.java:606)
         atorg.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:175)
         atorg.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:446)
         atorg.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:434)
         atorg.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)
         atorg.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
         atorg.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
         atorg.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
         atjavax.servlet.http.HttpServlet.service(HttpServlet.java:620)
         atorg.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
         atjavax.servlet.http.HttpServlet.service(HttpServlet.java:727)
         atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
         atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
         atorg.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
         atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
         atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
         atcom.alibaba.druid.support.http.WebStatFilter.doFilter(WebStatFilter.java:123)
         atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
         atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
         atorg.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:322)
         atorg.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:107)
         atorg.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)
         atorg.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
         atcom.dangdang.webx.core.security.access.intercept.UrlSecurityInterceptor.invoke(UrlSecurityInterceptor.java:87)
         atorg.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)
         atorg.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
         atorg.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
         atorg.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
         atorg.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)
         atorg.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
         atorg.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
         atorg.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
         atorg.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
         atorg.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
         atorg.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
         atorg.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
         atorg.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:182)
         atorg.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
         atorg.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:182)
         atorg.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
         atorg.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105)
         atorg.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
         atorg.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:65)
         atorg.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
         atorg.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
         atorg.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
         atorg.springframework.security.web.session.ConcurrentSessionFilter.doFilter(ConcurrentSessionFilter.java:125)
         atorg.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
         atorg.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:184)
         atorg.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:155)
         atorg.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:344)
         atorg.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:261)
         atcom.dangdang.webx.core.security.access.intercept.SecurityFilterProxy.doFilter(SecurityFilterProxy.java:71)
         atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
         atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
         atcom.dangdang.webx.core.security.context.ContextPersistenceFilter.doFilter(ContextPersistenceFilter.java:78)
         atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
         atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
         atorg.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
         atorg.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
         atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
         atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
         atorg.springframework.orm.hibernate4.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:150)
         atorg.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
         atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
         atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
         atorg.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
         atorg.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
         atorg.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
         atorg.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
         atorg.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
         atorg.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
         atorg.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
         atorg.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
         atorg.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
         atorg.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
         atorg.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
         atjava.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
         atjava.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
         atorg.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
         atjava.lang.Thread.run(Thread.java:745)
Caused by: groovy.lang.MissingMethodException:No signature of method: java.lang.String.intValue() is applicable for argumenttypes: () values: []
Possible solutions: intern()
         atorg.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:55)
         atorg.codehaus.groovy.runtime.callsite.PojoMetaClassSite.call(PojoMetaClassSite.java:46)
         atorg.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
         atorg.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
         atorg.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:112)
         atexpSumDeliveryNoPrint_1513825399653_142592.evaluate(calculator_expSumDeliveryNoPrint_1513825399653_142592:246)
         atnet.sf.jasperreports.engine.fill.JREvaluator.evaluate(JREvaluator.java:250)
         ...102 more

iReport功能还是蛮强大的,就是有点重。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值