JasperReports中的Groovy表达式:比较使用Java的枚举

时间:2013-09-30 10:56:47

标签: groovy jasper-reports

如果其中一个是Enum,是否可以在报告中使用 Groovy 语法比较两个 Java 对象?

目前我的报告在使用more Caused by: groovy.lang.MissingPropertyException: No such property: RequestServiceState

执行时失败了

表达式:

($P{service}.state.id == RequestState.Offer.id ? "true" : "false")

其中RequestState是:

public enum RequestState {
    New(1), InProcess(2), Pending(3), Offer(4), Order(5), Done(6);

    private Integer value;

    private RequestState(final Integer value) {  
        this.value = value;  
    }  

    public Integer getId() {  
        return this.value;  
    }  

    public static RequestState getValue(final Integer value) throws EnumValueNotFoundException {  
        switch (value) {  
            case 1:  
                return New;  
            case 2:  
                return InProcess;
            case 3:  
                return Pending;
            case 4:  
                return Offer;
            case 5:  
                return Order;
            case 6:  
                return Done;
            default:
                throw new EnumValueNotFoundException(value);
        }  
    }
}

全部进口。

1 个答案:

答案 0 :(得分:0)

您可以在表达式中使用枚举类型

我同意 tim_yates - 我认为您忘记在 jrxml 文件中为 RequestState 添加导入,这就是您捕获此异常的原因

sanBez 提到了另一个问题 - 您应该以正确的方式使用 Bean 。请参阅下面的详细信息。

使用枚举的样本。

jrxml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="test_enum" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="5950245d-5a85-43a8-a4f6-9e11b44e62d1">
    <import value="packagename.*"/>
    <parameter name="service" class="RequestBean" isForPrompting="false"/>
    <title>
        <band height="79" splitType="Stretch">
            <textField>
                <reportElement uuid="b5084124-1a2c-4bb9-ad93-503aa8474c12" x="122" y="28" width="336" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[($P{service}.state.id == RequestState.Offer.id ? "true" : "false")]]></textFieldExpression>
            </textField>
        </band>
    </title>
</jasperReport>

RequestState enum 代码为:

package packagename;

public enum RequestState {

    New(1), InProcess(2), Pending(3), Offer(4), Order(5), Done(6);

    private Integer value;

    private RequestState(final Integer value) {
        this.value = value;
    }

    public Integer getId() {
        return this.value;
    }

    public static RequestState getValue(final Integer value) throws IllegalStateException {
        switch (value) {
            case 1:
                return New;
            case 2:
                return InProcess;
            case 3:
                return Pending;
            case 4:
                return Offer;
            case 5:
                return Order;
            case 6:
                return Done;
            default:
                throw new IllegalStateException();
        }
    }
}

豆类在这里。 RequestBean 类:

package packagename;

public class RequestBean {

    private StateBean state;

    public RequestBean(StateBean state) {
        this.state = state;
    }

    public StateBean getState() {
        return state;
    }
}

StateBean 类:

package packagename;

public class StateBean {

    private Integer id;

    public StateBean(int id) {
        this.id = id;
    }

    public Integer getId() {
        return id;
    }
}

测试代码是:

public static void testReport(String reportSource, String outputFileName) throws JRException {
    Map<String, Object> params = new HashMap<String, Object>();

    params.put("service", new RequestBean(new StateBean(4)));

    JasperReport jasperReport = JasperCompileManager.compileReport(reportSource);
    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, new JREmptyDataSource());

    JasperExportManager.exportReportToPdfFile(jasperPrint, outputFileName);
}

结果将是(生成 pdf 文件):

enter image description here

<强> 备注

  1. 不要忘记将所有课程添加到 classpath
  2. 不要忘记在 jrxml 文件中添加导入(或使用类的全名)
  3. 不要忘记为 jvm 显示 Java Bean 的字段。查看有关使用JavaBean Data Sources
  4. 的详细信息