【问题标题】:How to dynamically refresh h:selectManyCheckbox selectItems如何动态刷新 h:selectManyCheckbox selectItems
【发布时间】:2011-04-02 20:19:24
【问题描述】:

我正在尝试使用 JSF 实现一个场景。我有一个commandExButton,当用户单击此按钮“A”时,它会显示包含selectManyCheckBox 项目的panelDialog。我通过解析一个不断更新的文件在后端 bean 中生成这些项目。我想要的是,每当我单击此按钮“A”时,我都应该通过后端 bean 解析文件来获取最新的 selectItems。但是我得到的是第一次呈现页面时生成的相同 selectItem。所以到目前为止,我有一个解决方法,其中我包含一个刷新按钮,它实际上刷新页面,然后用户单击“A”通过解析当前文件的内容来获取最新的 selectItem。但是在不添加新按钮和使用现有按钮的情况下无论如何都可行吗?

以下是我正在使用的代码

<td>
    <hx:commandExButton id="preferenceButton" styleClass="form" value="#{nls.preferenceLink}"
        title="#{nls.preferenceLinkTitle}" type="submit" />
</td>
<td>
    <h:form id="PrefForm" rendered="#{Test.isCurrent}">
        <hx:panelDialog type="modal" id="preferenceSet" styleClass="panelDialog" for="preferenceButton"
            title="#{nls.preferenceDialogTitle}">
            <h:outputText styleClass="panelStartMessage" style="display:block;"
                value="#{nls.preferenceDialogWindowText}" />
            <h:panelGroup rendered="#{Test.hasSelectItem }"
                style="display:block;width:300px;height:360px;overflow:auto;" styleClass="panelGroup"
                id="prefPanelGroup">
                <h:selectManyCheckbox value="#{Test.selectedItems}" layout="pageDirection">
                    <f:selectItems value="#{Test.selectItems}" />
                </h:selectManyCheckbox>
            </h:panelGroup>
            <hx:panelBox styleClass="information_box" id="noCommandWindow" layout="lineDirection"
                rendered="#{!Test.hasSelectItem }">
                <h:outputText styleClass="outputText" id="cmdInfo" value="#{nls.noCommands}" />
            </hx:panelBox>
            <hx:panelBox id="buttonBox1" styleClass="panelStartBox" layout="lineDirection">
                <hx:commandExButton id="submitPref" styleClass="commandExButton" type="submit"
                    value="#{nls.submit}" action="#{Test.action}">
                    <hx:behavior event="onclick" behaviorAction="hide" targetAction="preferenceSet"
                        id="behaviorSubmitPref" />
                </hx:commandExButton>
                <hx:commandExButton id="CancelPref" styleClass="commandExButton" type="submit"
                    value="#{nls.cancel}" action="Test">
                    <hx:behavior event="onclick" behaviorAction="hide" targetAction="preferenceSet"
                        id="behaviorCancelPref" />
                </hx:commandExButton>
            </hx:panelBox>
        </hx:panelDialog>
    </h:form>
</td>

Bean 中的代码:

public class Test{
    private List<String> selectedItems;
    private List<SelectItem> selectItems;

    public List<SelectItem> getSelectItems() {
        // populate the selectItem here...
    }    
}

【问题讨论】:

    标签: jsf jsf-2 selectmanycheckbox


    【解决方案1】:

    List&lt;SelectItem&gt; selectItems 存储在一个单独的会话范围bean 中(假设您当前的bean 是请求范围的),在其构造过程中填充它并添加一个类似reload() 的方法,您只在之后调用在 action 方法中处理选定的项目。您可以从@ManagedProperty 范围内的请求中访问会话范围的bean。

    【讨论】:

    • 您好 BalusC,感谢您的回复。我当前的 bean 仅在会话范围内。我尝试将 action 和 actionListener 方法绑定到 selectItems,然后在该面板对话框中显示最新的复选框选择项。但是按钮根本没有调用bean方法,而我实际上被卡住了。还有其他方法吗?
    • “在构建过程中填充它并添加一个类似 reload() 的方法”——那么我应该绑定哪个组件?我应该添加另一个按钮并附加这个刷新按钮吗?但在那种情况下,它会刷新整个页面,我想避免这种情况。我是 JSF 的新手,所以如果我在这里不明白任何明显的东西,请忍受我的有限知识。
    猜你喜欢
    • 2012-05-08
    • 1970-01-01
    • 2013-09-16
    • 1970-01-01
    • 1970-01-01
    • 2020-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多