EL语法,用于检查集合是否包含特定的Enum值 [英] EL syntax to check if a set contains a specific Enum value

查看:232
本文介绍了EL语法,用于检查集合是否包含特定的Enum值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Item对象,该对象具有一个字段,该字段是一组ItemTypes:

I have an Item object, which has a field that is a Set of ItemTypes:

public class Item {
    EnumSet<ItemType> itemTypeSet;

    ...

    public Set<ItemType> getItemTypeSet(){
        return this.itemTypeSet;
    }
}

ItemType当然是一个简单的枚举.

ItemType is of course a simple Enum.

public Enum ItemType {
    BOLD, THIN, COOL, ROUND;
}

在我的JSP中,我想使用JSTL来查看某项是否具有特定的ItemType,我尝试使用以下三个代码段,但没有错误也没有结果.我不知道为什么这三个都失败了.有人可以针对这3种情况分别说明为什么该程序无法像我认为的那样工作,并提供了第四个可行的替代方法:)?

In my JSP I would like to use JSTL to see if an item has a specific ItemType, I tried to use the following three snippets but I get no errors and no results. I'm not sure why all 3 are failing. Could somebody explain, for each of these 3 cases, why the program doesn't work like I think it does, and provide a 4th alternative that's working :)?

<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的类,都可以完全访问它.

Important is that the ItemType enum is public and not inside another class. It is totally accessible for any other class, including the ones that resolve the EL/JSTL/JSP.

请注意,遍历枚举集中的所有值都很好:

Note that iterating over all values in the enumset works fine:

<c:forEach items="${item.itemTypeSet}" var="itemType">
    <p>${itemType}</p>
</c:forEach>

给出结果:

BOLD
ROUND

推荐答案

方法3将在EL 3.0中运行(Tomcat 8,WildFly 8,GlassFish 4等,以及更高版本),前提是您按照指示将枚举导入到JSP页面上下文中在如何在EL中引用常量?

Method 3 will work in EL 3.0 (Tomcat 8, WildFly 8, GlassFish 4, etc and newer) provided that you imported the enum in JSP page context as instructed in How to reference constants in EL?

<@page import="com.example.ItemType" %>

方法2在理论上也应该在EL 3.0中工作,但是与3相比,它比较笨拙.

Method 2 should theoretically work in EL 3.0 too, but it's clumsy as compared to 3.

方法1根本不起作用,因为EL不了解该集合的通用类型,并且由于${'...'}语法的缘故,仍然假定它是普通的String.基本上,这是在幕后进行的"BOLD".equals(BOLD),这是永远不会过去的.

Method 1 won't work at all as EL is not aware of the generic type of the collection and still assumes it plain String due to the ${'...'} syntax. Basically, it's doing "BOLD".equals(BOLD) under the covers which will never pass.

最好在此之上创建一个自定义的EL函数.有关自定义EL功能的启动示例,请转到以下相关问题的答案:

You'd best create a custom EL function on this. For a kickoff example of a custom EL function, head to the answer on this related question: How can i do a multiselect in jsp/jstl with selected value? You'd like to end up with something like this:

<c:if test="${my:containsEnum(item.itemTypeSet, 'BOLD')}">

然后在containsEnum(Set, String)函数中相应地执行Java魔术.

And do the Java magic accordingly in the containsEnum(Set, String) function.

这篇关于EL语法,用于检查集合是否包含特定的Enum值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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