在JSP EL中,枚举值始终为空 [英] In JSP EL enum value always empty

查看:146
本文介绍了在JSP EL中,枚举值始终为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当尝试获得EL条件工作时,我发现枚举值被完全忽略。这似乎与我相反。

When trying to get an EL condition working I found that enum values are completely ignored. This seems to me contrary to the spec.

<c:out value='${com.foobar.data.BookingStatus.FAILED}' />
<c:out value='${BookingStatus.FAILED}' />
<c:out value='${com.foobar.data.BookingStatus.failed}' />
<c:out value='${BookingStatus.failed}' />
<c:if test="${empty BookingStatus.FAILED }">empty</c:if>

令我惊讶的是,这些都评估为。为什么不能识别枚举类?
这是在当前稳定的Tomcat实例中发生的。

To my surprise these all evaluate to empty. Why is the Enum class not recognized? This is happening in a current stable Tomcat instance.

这可能是一个类路径问题吗?枚举在控制器代码中成功使用,但在JSP中没有其他位置。它在部署的lib目录中的jar中提供。

Can this be a classpath issue? The Enum is used successfully in controller code but nowhere else in JSPs. It is supplied in a jar in the lib directory of the deployment.

更新:

我的目的是将一个提供的整数与一个枚举的属性进行比较:

My intention is to compare a supplied Integer to an Enum's property like this:

<c:when test='${bookingInformation.bookingStatus eq BookingStatus.FAILED.code}'>
    FOOBARFAIL
</c:when>

不幸的是,被检查的值不能更改,并将保持为整数。枚举看起来如下(简化):

Unfortunately the value being checked can't be changed and will remain an Integer. The Enum looks as follow (simplified):

public enum BookingStatus {

    COMPLETED(0), FAILED(1);

    private final int code;

    private BookingStatus(int code) {
        this.code = code;
    }

    public int getCode() {
        return code;
    }

}

我想避免硬代码FAIL等的整数值,并使用枚举代替比较。

I want to avoid to hard code the Integer value of FAIL etc. and use the enum instead for the comparison.

推荐答案

这是因为当前版本的EL不支持访问枚举或调用枚举常量。此支持仅适用于每个EL 3.0。

That's because EL in its current version does not support accessing enums nor calling enum constants. This support is only available per EL 3.0.

目前还不清楚您的意图是什么,但很高兴知道您可以将枚举属性作为 EL中的字符串。它们被解析为 String

It's unclear what your intent is, but it's good to know that you can compare enum properties as a String in EL. They are namely resolved as a String.

假设你有一个如下所示的bean:

Assuming that you've a bean which look like this:

public class Booking {
    public enum Status { NEW, PROGRESS, SUCCESS, FAILED }

    private Status status;

    public Status getStatus() {
        return status;
    }
}

然后你可以测试 Status.FAILED 条件如下:

Then you could test the Status.FAILED condition as follows:

<c:if test="${booking.status == 'FAILED'}">
    Booking status is FAILED.
</c:if>



另请参见:




  • 如何引用EL中的常量

  • See also:

    • How to reference constants in EL?
    • 这篇关于在JSP EL中,枚举值始终为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆