【问题标题】:<p:dialog> rendered property not getting updated using <p:commandLink update=":dialog"><p:dialog> 渲染属性未使用 <p:commandLink update=":dialog"> 更新
【发布时间】:2013-10-11 12:48:47
【问题描述】:

我有一个&lt;h:dataTable&gt;,其中有一个&lt;p:commandLink&gt;。 我需要在&lt;p:commandLink&gt; 时从数据库中获取一些数据 单击并在我使用&lt;p:dialog&gt;的弹出窗口中显示它。

<h:form id="form">
<h:dataTable width="80%" value="#{controller.items}" var="a" binding="#{bean.table}"
                rendered="#{not empty controller.items}">
        <h:column>
            <h:outputText value="#{a.date}" />
        </h:column>

        <h:column>
            <h:outputText value="#{a.name}" />
        </h:column>

        <h:column>
            <p:commandLink value="View" action="#{controller.getData()}"
                        update=":form:dialog" oncomplete="w_dialog.show();return false;">

            </p:commandLink>
        </h:column>

        </h:dataTable>

            <p:dialog header="Test" widgetVar="w_dialog" width="600px" height="500px"
                    id="dialog" modal="true" draggable="true" appendToBody="true" rendered="#{sessionScope.sample ne null}">
                    <ui:include src="sample.xhtml"/>
            </p:dialog>

</h:form>   

我需要捕获被点击的行的数据并从数据库中获取数据。 我的bean和控制器类如下:

@Named
@SessionScoped
public class Bean implements Serializable
{       
    private HtmlDataTable table;

    // getters and setters                                      

}


@Named
@SessionScoped
public class Controller implements Serializable
{       

    @Inject
    private Bean bean;

    public void getData(){

    bean.getTable().getRowData();
    SampleClass sample=new SampleClass();


    // fetches data from database and populates it within sample instance

    FacesContext context = FacesContext.getCurrentInstance();
    context.getExternalContext().getSessionMap()
                .put("sample", sample);

    }

}

&lt;p:dialog&gt; 包含一个名为 sample.xhtml 的文件,其中包含引用 到SampleClass。所以我在&lt;p:dialog&gt; 中使用了rendered 属性来避免 NullPointer Exception 在加载我的 xhtml 页面时。此外,SampleClass 类型的 sample 被插入到 sessionMap 仅在控制器方法getData() 执行后单击 &lt;p:commandLink&gt;

问题是即使在方法之后弹出窗口也不会显示 getData() 被执行,sample 被插入到 SessionMap 中。

我使用update=:form:dialog 更新&lt;p:commandLink&gt; 之后的对话框 被点击。但似乎对话框的 rendered 属性永远不会得到 更新。所以我看不到&lt;p:dialog&gt;

我错过了什么吗?

【问题讨论】:

    标签: jsf-2 primefaces dialog


    【解决方案1】:

    您无法更新不存在的组件。 rendered 属性决定组件是否显示在 DOM 树中,而不仅仅是它的可见性。这意味着,如果为 false,则此组件将不可用于 JSF,用于重新呈现/更新术语。

    对此的标准解决方案是将组件包装到容器元素中并改为更新它(顺便说一下,我鼓励您不要将getter 方法用于操作目的):

    <h:panelGroup id="parentPanel">
        <p:dialog header="Test" widgetVar="w_dialog" width="600px" height="500px"
            id="dialog" modal="true" draggable="true" 
            appendToBody="true" rendered="#{sessionScope.sample ne null}">
            <ui:include src="sample.xhtml"/>
        </p:dialog>
    </h:panelGroup>
    
    <p:commandLink value="View" action="#{controller.showData()}"
                            update=":form:parentPanel" />
    

    【讨论】:

    • 现在正在显示弹出窗口,但 JSF 页面未显示会话应对变量 sample 的更新值。相反,它显示第一次单击&lt;p:commandLink&gt; 时填充的相同值。我显示值的方法有问题吗?
    • 使用ui:include 意味着如果页面是@ViewScoped,它将在构建时包含在内,您可能无法通过ajax 更新其内部值(我不确定)。但是,您有 more ways 包含一些并不意味着在构建时执行此操作的内容。尝试将您的sample.xhtml 的内容直接放在对话框中。如果可行,请尝试使用模板。
    • 它有效,我在&lt;p:dialog&gt; 中使用appendToBody=true,因此更新的对话框显示在第一个对话框下(第一次单击&lt;p:commandLink&gt; 打开的对话框) .就像,如果我们点击&lt;p:commandLink&gt; 3 次,就会显示 3 个对话框,一个在另一个之下。因此,sessionScope 中的变量 sample 实际上每次都在 JSF 页面中更新。问题出在dailog上。感谢您的帮助...!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-18
    • 2011-11-18
    • 1970-01-01
    • 1970-01-01
    • 2020-12-16
    • 2021-09-04
    相关资源
    最近更新 更多