如何获取jsf f所选项目的索引:selectItems?

Ara*_*yan 0 jsf jsf-2 selectoneradio

我有seleconeradio,例如:

<h:selectOneRadio value="#{myBean.selectedValue}" layout="pageDirection">
    <f:selectItems value="#{myBean.myList}" var="a" itemValue="#{a}" itemLabel="#{a}"/>
</h:selectOneRadio>
Run Code Online (Sandbox Code Playgroud)

其中myList是整数列表,例如1,3,2,4.如果用户在myBean selectedValue中选择第二个元素(即3),那么我想得到selectItems项的索引.

我应该在f:selectItems itemValue标签中写什么?或者这是不可能的?

PS我可以通过创建一个新类来实现它,在该类中我有索引属性并创建该类的新列表,给出正确的索引.但这是非常糟糕的解决方案.

Dan*_*elK 5

你实际上可以c:forEach用于这种情况.当您必须处理包含重复项的集合时,这尤其有用,因此无法使用indexOf().

<h:selectOneRadio value="#{myBean.selectedValue}" layout="pageDirection">
  <c:forEach items="#{myBean.myList}" var="a" varStatus="idx">
    <f:selectItem itemValue="#{idx.index}" itemLabel="#{a}"/>
  </c:forEach>
</h:selectOneRadio>
Run Code Online (Sandbox Code Playgroud)

如果还没有完成,请确保包含JSP JSTL Core命名空间.

xmlns:c="http://xmlns.jcp.org/jsp/jstl/core