我有一个 Item 对象,它有一个字段是一组 ItemTypes:
public class Item {
EnumSet<ItemType> itemTypeSet;
...
public Set<ItemType> getItemTypeSet(){
return this.itemTypeSet;
}
}
ItemType 当然是一个简单的枚举。
public Enum ItemType {
BOLD, THIN, COOL, ROUND;
}
在我的 JSP 中,我想使用 JSTL 来查看一个项目是否具有特定的 ItemType,我尝试使用以下三个片段,但没有得到任何错误和结果。我不确定为什么所有三个都失败了。有人可以解释一下,对于这三种情况,为什么该程序不像我认为的那样工作,并提供第四个可行的替代方案:)?
<c:if test="${item.itemTypeSet.contains('BOLD')}">
Method 1 works!
</c:if>
<c:if test="${item.itemTypeSet.contains(ItemType.valueOf('BOLD'))}">
Method 2 works!
</c:if>
<c:if test="${item.itemTypeSet.contains(ItemType.BOLD)}">
Method 3 works!
</c:if>
重要的是ItemType
枚举是公共的,而不是在另一个类中。任何其他类都可以完全访问它,包括解析 EL/JSTL/JSP 的类。
请注意,遍历枚举集中的所有值都可以正常工作:
<c:forEach items="${item.itemTypeSet}" var="itemType">
<p>${itemType}</p>
</c:forEach>
结果:
BOLD
ROUND