简体   繁体   English

如何在JSF EL中创建数组?

[英]How to create an array in JSF EL?

I want create an array in JSF EL. 我想在JSF EL中创建一个数组。 How can I do that? 我怎样才能做到这一点? Is it even possible? 它甚至可能吗?

To illustrate what I am trying: 为了说明我在尝试什么:

<rich:pickList addAllText="" addText="" removeAllText="" removeText="">
    <f:selectItems value="#{'Test', 'TestTest', 'TestTestTest'}" />
</rich:pickList>

If you're on EL 3.0 or newer, you can construct collections directly in EL . 如果您使用的是EL 3.0或更高版本,则可以直接在EL中构建集合

<f:selectItems value="#{['Test','TestTest','TestTestTest']}" />

If you're not on EL 3.0 yet, you could solve this particular case with a fn:split() trick. 如果你还没有使用EL 3.0,你可以使用fn:split()技巧解决这个特殊情况

<html ... xmlns:fn="http://java.sun.com/jsp/jstl/functions">
...
<f:selectItems value="#{fn:split('Test,TestTest,TestTestTest', ',')}" />

Either way, this requires a minimum of JSF 2.0 for the support of List<T> in <f:selectItems> . 无论哪种方式,这需要最少的JSF 2.0来支持<f:selectItems>List<T> <f:selectItems>

It's possible with EL 3.0: EL 3.0可以实现:

[1, 2, 3].toArray()

This creates a List first, and then converts it to an array. 这首先创建一个List,然后将其转换为数组。

At least with recent JSF versions you don't even need an array for f:selectItems , any Iterable will do. 至少在最近的JSF版本中,你甚至不需要f:selectItems的数组,任何Iterable都可以。 So: 所以:

<f:selectItems value="#{['Test', 'TestTest', 'TestTestTest']}" />

For more information about collection construction in EL see the EL 3.0 Specification - final release , 2.2 Construction of Collection Objects. 有关EL中集合构造的更多信息,请参阅EL 3.0规范 - 最终版本 ,2.2构建集合对象。

I am using ArrayUtils from apache to do this. 我正在使用apache的ArrayUtils来做到这一点。 first, register the ArrayUtils class as a bean so you can access it from EL. 首先,将ArrayUtils类注册为bean,以便从EL访问它。

<managed-bean>
    <managed-bean-name>arrayUtils</managed-bean-name>
    <managed-bean-class>org.apache.commons.lang3.ArrayUtils</managed-bean-class>
    <managed-bean-scope>application</managed-bean-scope>
</managed-bean>

then you can call ArrayUtils' methods: 然后你可以调用ArrayUtils的方法:

#{myBean.aMethod(arrayUtils.add(ArrayUtils.EMPTY_INT_ARRAY, 1))}

the above code will not work if you can't access the constant EMPTY_INT_ARRAY. 如果您无法访问常量EMPTY_INT_ARRAY,则上述代码将不起作用。 To this, i used primefaces p:importConstants component, not sure if can be done in plain JSF. 为此,我使用了primefaces p:importConstants组件,不确定是否可以在普通的JSF中完成。

this is how i use it: 这是我如何使用它:

<p:importConstants type="org.apache.commons.lang3.ArrayUtils" var="ArrayUtils" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM