1

我正在尝试使用 JSF 解决这里的一些问题,但我没有运气。我会尝试恢复我的代码,因为我不认为这里的很多代码可以帮助你解决这个问题,所以我会尝试更好地描述我的问题。

现在我有一个存储三个班次的字符串:matutinal、vespertine 和 nightly。在我的架构中,我需要myStringArray[0] = 'matutinal'myStringArray[1] = 'vespertine'并且myStringArray[3] = 'nightly'.

我在我的应用程序中使用 JSF 2.0 和 Primefaces - 也有一些omnifaces

以下是我的 JSF 代码:

<p:selectManyCheckbox value="#{escolaMBean.turnos}">
    <f:selectItems value="#{escolaMBean.listaTodosTurnos}" var="turno" itemValue="#{turno.nome}" itemLabel="#{turno.nome}" />                                       
</p:selectManyCheckbox>

escolaMBean 中的注意事项:

// Stores the selected "Turnos" (This means "shift" in English)
String[] turnos = new String[3];

// Stores all the "Turnos" received from DB
ArrayList<Turno> listaTodosTurnos = <myControl.myDbRequest()>

/*
* Turno have a simple ID and Name, in DB we have 3 "Turnos": Matutinal, Vespertine, Nightly
* In this MBean I have all getters and setters - and in "Turno" class too.
* When I set one string in turnos[n], this set the right value
*/

那么,基于这些事情,如果选择了 matutinal 复选框,我该如何选择 turnos[0],如果选择了vespertine复选框,我该如何选择 turnos[0],如果选择了每晚复选框,我该如何选择 turnos[2]?现在这不起作用,因为如果我先选择 Nightly,则位置 turnos[0] 将等于“nigthly”。

我该如何解决这个问题?

4

1 回答 1

1

标准 JSF 方法无法实现您想要的。您受到 HTML 工作方式的限制。HTML<input type="checkbox">仅提交有关选定值的信息,而不提交有关未选定值的信息。JSF 在这里只是 HTML/HTTP 和 Javabean 模型之间的信使。所有 JSF 检索都是选定值的集合。它不会检索未选择值的集合。

您需要根据自己选择的值与可用值中未选择的值相交。

这是一个启动示例,假设您有一个

private List<String> selectedItems; // <p:selectManyCheckbox value>
private List<Item> availableItems; // <f:selectItems value>
private String[] orderedSelectedItems; // Selected items ordered by index

那么这应该做,例如提交表单后的动作监听器:

orderedSelectedItems = new String[availableItems.size()];
int i = 0;

for (Item item : availableItems) {
    String name = item.getName();
    orderedSelectedItems[i++] = selectedItems.contains(name) ? name : null;
}
于 2013-11-23T10:47:28.557 回答