集合/数组包含方法 [英] Collection/Array contains method
问题描述
我想知道是否有包含方法的集合/数组在EL 2.2
否则我将不得不做出一个自定义的?
i was wondering if there's contains method for collections/array in EL 2.2 or i will have to make a custom one ?
要求金额:我有一个字符串数组,我想找到它是否包含一个特定的字符串
REQUIREMENT: i have a string array, and i want to find if it contains a specific string.
CASE :我正在循环上的输入复选框,以使它们的名单,我要检查当前复选框,如果它的价值复选框数组中存在。
CASE: i am looping on list of input checkboxes to render them, and i want to check the current checkbox, if it's value exists in the array of checkboxes.
更新
-
在EL是这样的方法可用?
is such method is available in EL?
如果这样的方法不可用,那么请提供您的最佳性能作为一个字符串数组方法中包含的元素。建议
If such method is not available, then please provide your suggestion for best performance method for a string array contains an element.
推荐答案
对于收藏
这很容易,只需要使用 Colleciton#包括()
在EL方法。
For a Collection
it's easy, just use the Colleciton#contains()
method in EL.
<h:panelGroup id="p1" rendered="#{bean.panels.contains('p1')}">...</h:panelGroup>
<h:panelGroup id="p2" rendered="#{bean.panels.contains('p2')}">...</h:panelGroup>
<h:panelGroup id="p3" rendered="#{bean.panels.contains('p3')}">...</h:panelGroup>
对于对象[]
(阵列),你需要一个最低EL 3.0和利用其新的lambda的支持。
For an Object[]
(array), you'd need a minimum of EL 3.0 and utilize its new Lambda support.
<h:panelGroup id="p1" rendered="#{bean.panels.stream().anyMatch(v -> v == 'p1').get()}">...</h:panelGroup>
<h:panelGroup id="p2" rendered="#{bean.panels.stream().anyMatch(v -> v == 'p2').get()}">...</h:panelGroup>
<h:panelGroup id="p3" rendered="#{bean.panels.stream().anyMatch(v -> v == 'p3').get()}">...</h:panelGroup>
如果您在EL 3.0是还没有,你需要创建一个自定义EL函数。对于一个具体的例子,参见<一href=\"http://stackoverflow.com/questions/7079978/how-to-create-a-custom-el-function/7080174#7080174\">How创建一个自定义EL函数?例如
If you're not on EL 3.0 yet, you'd need to create a custom EL function. For a concrete example, see How to create a custom EL function? E.g.
public static boolean contains(Object[] array, Object item) {
return Arrays.asList(array).contains(item);
}
被登记为
<function>
<function-name>contains</function-name>
<function-class>com.example.Functions</function-class>
<function-signature>boolean contains(java.lang.Object[], java.lang.Object)</function-signature>
</function>
和用作
<h:panelGroup ... rendered="#{func:contains(bean.panels, 'u1')}">
这是不是在JSTL可用。有一个 FN :包含()
,但适用于字符串
值仅
This is not available in JSTL. There's a fn:contains()
, but that works on String
values only.
这篇关于集合/数组包含方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!