在报告中显示一个枚举值

时间:2016-09-07 04:50:46

标签: java jasper-reports

我有一个字段

的报告
<field name="Gender" class="java.lang.Object">
    <fieldDescription><![CDATA[gender]]></fieldDescription>
</field>

性别是一个枚举字段

public enum Gender {
    MALE, FEMALE
}

在报告详细信息区域中,我想显示性别

        <textField isStretchWithOverflow="true" isBlankWhenNull="true">
            <reportElement style="Detail" x="6716" y="0" width="144" height="29"/>
            <textElement verticalAlignment="Middle" lineSpacing="Single"/>
            <textFieldExpression class="java.lang.String"><![CDATA[$F{Gender}]]></textFieldExpression>
        </textField>

但textField只显示enum.male.MALE或enum.female.FEMALE。如何显示文字MALE或FEMALE仅取决于性别价值?

将textField代码修改为以下内容时:

        <textField isStretchWithOverflow="true" isBlankWhenNull="true">
            <reportElement style="Detail" x="6716" y="0" width="144" height="29"/>
            <textElement verticalAlignment="Middle" lineSpacing="Single"/>
            <textFieldExpression class="java.lang.String"><![CDATA[($F{Gender} == enum.male.MALE) ? "Male" :
                                                             (($F{Gender} == enum.female.FEMALE) ? "Female" :
                                                               "No gender")]]></textFieldExpression>
        </textField>

我收到以下错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jasperViewResolver' defined in class path resource [spring/webmvc-config.xml]: Invocation of init method failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myreport' defined in ServletContext resource [/WEB-INF/reports/reportviews.xml]: Initialization of bean failed; nested exception is org.springframework.context.ApplicationContextException: Could not parse JasperReports report from ServletContext resource [/WEB-INF/reports/OL001_Opiskelijalista.jrxml]; nested exception is net.sf.jasperreports.engine.JRException: Errors were encountered when compiling report expressions class file:
1. Syntax error on token "enum", invalid Expression
                value = (java.lang.String)((((java.lang.Object)field_Gender.getValue()) == enum.male.MALE) ? "Male" : //$JR_EXPR_ID=120$
                                                                                           <-->
2. Syntax error on token "enum", invalid Expression
                                                                 ((((java.lang.Object)field_Gender.getValue()) == enum.female.FEMALE) ? "Female" : //$JR_EXPR_ID=120$
                                                                                                                  <-->
3. Syntax error on token "enum", invalid Expression
                value = (java.lang.String)((((java.lang.Object)field_Gender.getOldValue()) == enum.male.MALE) ? "Male" : //$JR_EXPR_ID=120$
                                                                                              <-->
4. Syntax error on token "enum", invalid Expression
                                                                 ((((java.lang.Object)field_Gender.getOldValue()) == enum.female.FEMALE) ? "Female" : //$JR_EXPR_ID=120$
                                                                                                                     <-->
5. Syntax error on token "enum", invalid Expression
                value = (java.lang.String)((((java.lang.Object)field_Gender.getValue()) == enum.male.MALE) ? "Male" : //$JR_EXPR_ID=120$
                                                                                           <-->
6. Syntax error on token "enum", invalid Expression
                                                                 ((((java.lang.Object)field_Gender.getValue()) == enum.female.FEMALE) ? "Female" : //$JR_EXPR_ID=120$
                                                                                                                  <-->
6 errors

1 个答案:

答案 0 :(得分:0)

由于枚举不是文本类型,因此您必须检查枚举并打印出字符串值。

通过在文本字段中使用此表达式来实现此目的的一种方法:

($F{Gender}.getClass().getName().equals("enum.male.MALE") ? "Male" : 
  (($F{Gender}.getClass().getName().equals("enum.male.FEMALE")) ? "Female" : 
    "No gender")

使用此语句,您可以将枚举映射到某个任意字符串值。