我正在使用支持EL 3.0的Wildfly 10.1,因此支持EL中的常量(How to reference constants in EL?)。我有一个内部枚举的课程。
package com.abc.def.helpers;
import java.util.HashMap;
import java.util.Map;
public class MyConstants {
public enum BusinessObject {
CALENDAR ("Calendar"),
PROJECT ("Project");
private final String friendlyName;
private static final Map<String, BusinessObject> friendlyNameToEnum = new HashMap<>();
private BusinessObject(String value) {
friendlyName = value;
}
static {
for (BusinessObject businessObject : values()) {
friendlyNameToEnum.put(businessObject.toString(), businessObject);
}
}
@Override
public String toString() {
return friendlyName;
}
public static BusinessObject fromString(String friendlyName) {
return friendlyNameToEnum.get(friendlyName);
}
}
}
我有一个包含以下内容的JSP文件。
<%@ page import="com.abc.def.helpers.MyConstants.*" %>
Constant scriplet: [<%= BusinessObject.CALENDAR.toString() %>]<br>
Constant EL: [${BusinessObject.CALENDAR}]<br>
结果是:
Constant scriplet: [Calendar]
Constant EL: []
如何通过EL检索值?