【问题标题】:How to set a selectItems to a given String position?如何将 selectItems 设置为给定的字符串位置?
【发布时间】:2013-11-23 03:54:15
【问题描述】:

我正在尝试使用 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 复选框,我如何选择 turnos1,如果选择了每晚复选框,我该如何选择 turnos[2]?现在这不起作用,因为如果我先选择 Nightly,则位置 turnos[0] 将等于“nigthly”。

我该如何解决这个问题?

【问题讨论】:

    标签: arrays jsf jsf-2 primefaces selectmanycheckbox


    【解决方案1】:

    标准 JSF 方法无法实现您想要的。您受到 HTML 工作方式的限制。 HTML &lt;input type="checkbox"&gt; 仅提交有关选定值的信息,而不提交有关未选定值的信息。 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;
    }
    

    【讨论】:

    • 再次感谢 BalusC。我做了一些改编,但这行得通!
    猜你喜欢
    • 2013-09-04
    • 1970-01-01
    • 1970-01-01
    • 2016-07-10
    • 2021-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多