java 在 JSP EL 枚举值始终为空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4606673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In JSP EL enum value always empty
提问by sibidiba
When trying to get an EL condition working I found that enum values are completely ignored. This seems to me contrary to the spec.
当试图让 EL 条件工作时,我发现枚举值被完全忽略。在我看来,这与规范相反。
<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>
To my surprise these all evaluate to empty. Why is the Enum class not recognized? This is happening in a current stable Tomcat instance.
令我惊讶的是,这些都评估为空。为什么无法识别 Enum 类?这发生在当前稳定的 Tomcat 实例中。
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.
这可能是类路径问题吗?Enum 在控制器代码中成功使用,但在 JSP 中没有其他地方。它在部署的 lib 目录中的 jar 中提供。
UPDATE:
更新:
My intention is to compare a supplied Integer to an Enum's property like this:
我的目的是将提供的 Integer 与 Enum 的属性进行比较,如下所示:
<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;
}
}
I want to avoid to hard code the Integer value of FAIL etc. and use the enum instead for the comparison.
我想避免对 FAIL 等的整数值进行硬编码,而是使用枚举进行比较。
回答by BalusC
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 在其当前版本中不支持访问枚举或调用枚举常量。此支持仅适用于每个 EL 3.0。
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
.
不清楚您的意图是什么,但很高兴知道您可以将枚举属性与String
EL 中的a 进行比较。它们被解析为String
.
Assuming that you've a bean which look like this:
假设您有一个如下所示的 bean:
public class Booking {
public enum Status { NEW, PROGRESS, SUCCESS, FAILED }
private Status status;
public Status getStatus() {
return status;
}
}
Then you could test the Status.FAILED
condition as follows:
然后你可以测试Status.FAILED
条件如下:
<c:if test="${booking.status == 'FAILED'}">
Booking status is FAILED.
</c:if>
See also:
也可以看看:
回答by Steven Benitez
As BalusC indicated, you cannot access enums using EL, however, you can do this:
正如 BalusC 所指出的,您不能使用 EL 访问枚举,但是,您可以这样做:
<c:set var="enumFailed" value="<%=BookingStatus.FAILED%>"/>
<c:if test="${enumFailed.code == bookingInformation.bookingStatus}">
...
</c:if>
It would be ideal if bookingInformation.bookingStatus was an enum and not an int, but if re-factoring your app is out of the question due to its legacy nature, then my above example should help. You'd need a <c:set/>
for each value of the enum (appears to just be two in your example).
如果bookingInformation.bookingStatus 是一个枚举而不是一个int,那将是理想的,但如果由于其遗留性质,重构你的应用程序是不可能的,那么我上面的例子应该会有所帮助。<c:set/>
对于枚举的每个值,您都需要一个(在您的示例中似乎只有两个)。
回答by George Siggouroglou
You have to import the enum class in your jsp page. As far as you import it then you can refer to it. I wrote an example below.
您必须在 jsp 页面中导入 enum 类。至于你导入它,那么你可以参考它。我在下面写了一个例子。
My enum is the WebSettingType.
我的枚举是 WebSettingType。
public enum WebSettingType {
SMTP_HOSTNAME("smtp_hostname"),
SMTP_PORT("smtp_port"),
SMTP_USERNAME("smtp_username");
private final String value;
private WebSettingType(String value) {
this.value = value;
}
public String getValue() {
return value;
}
}
I have the websettings.jsp page that is uses a tag page etc.
我有使用标记页面等的 websettings.jsp 页面。
<%@page import="my.package.WebSettingType"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="t" tagdir="/WEB-INF/tags" %>
<t:admin>
<jsp:attribute name="css">
</jsp:attribute>
<jsp:attribute name="content">
<input type="text" name="${WebSettingType.SMTP_HOSTNAME.getValue()}"/>
</jsp:attribute>
</t:admin>