使用RequestScoped Bean和rendered属性的正确方法是什么?

使用RequestScoped Bean和rendered属性的正确方法是什么?

问题描述:

有人知道如何在jsf中将RequestScoped bean与渲染属性一起使用吗?呈现的属性在applyValues阶段之前进行评估,因此无法正确评估.我不想保留任何状态.该示例可以是带有数据表和按钮的outputPanel.数据表获取值列表.包装的outputPanel具有呈现的属性,如:

does anybody know how to use RequestScoped bean together with rendered attribute in jsf? The rendered attribute is evaluated before applyValues phase and therefore is not correctly evaluated. I don't want to preserve any state. The example could be an outputPanel with a datatable and a button. The datatable gets a list of values. The wrapping outputPanel has the rendered attribute like:

<p:outputPanel rendered="#{not empty requestScopedBean.dataList}">
    <p:datatable value="#{requestScopedBean.dataList}">
        ...
    </p:datatable>

    <p:commandButton action="#{requestScopedBean.someAction}" />
</p:outputPanel>

在加载页面并单击按钮之后,什么也没有发生,因为恢复了视图并评估了表达式-Bean确实具有空的数据列表,因此不应渲染面板.这会导致甚至没有调用该操作方法-因为该按钮不存在.

After loading the page and clicking on the button, nothing happens, because the view is restored and expressions are evaluated - the bean does have an empty datalist and therefore the panel should not be rendered. This causes that the action method is not even called - because the button doesn't exist.

如果您当时不希望填充数据表,请在 rendered 属性中添加额外的检查,如果感兴趣的命令按钮已被调用.您可以通过在请求参数映射中检查按钮的客户端ID的存在来做到这一点.

If you're not interested in having a filled data table at that moment, just add an extra check in rendered attribute if the command button of interest has been invoked. You can do that by checking the presence of button's client ID in request parameter map.

<p:outputPanel rendered="#{not empty requestScopedBean.dataList or not empty param[someButton.clientId]}">
    ...

    <p:commandButton binding="#{someButton}" ... />
</p:outputPanel>

另请参见: