【问题标题】:p:outputPanel does not render page sectionsp:outputPanel 不呈现页面部分
【发布时间】:2013-07-19 11:09:45
【问题描述】:

我有两个 p:ouputPanels 具有 rendered 属性。当底层getter/setter 的值发生变化时,它们应该被渲染。但是,基础价值会以正确的顺序发生变化。第一个面板消失了,但是第二个面板没有出现。甚至不在 HTML 代码中!

我的两个面板:

    <p:outputPanel id="PanelParent">

     <h:form>
        <p:outputPanel id="PanelForm" rendered="#{PageService.isVisibilityForm()}">

        <h:commandButton value="Button"
                                            action="#{PageService.persist}"
                                            styleClass="btn btn-primary opal_btn submit_form_new" />
     </h:form>                      
        </p:outputPanel>

        <p:outputPanel id="PanelMessage" rendered="#{PageService.isVisibilityMessage()}">

        //Message

        </p:outputPanel>

    </p:outputPanel>

感谢你的回答!!!

-更新-

在代码中,我引用了 persist 方法,在这个方法中,我更改了每个部分的可见性的 getter 和 setter。

-UPDATE1-

好的,这实际上是我的代码:

            <p:outputPanel id="parentPanel">
                <p:outputPanel id="PanelForm"
                    rendered="#{PageService.isVisibilityForm()}">
                    <div>
                        <div>
                            <h:form class="homepage_invitee_form" action="" method="POST">

                                <p:messages id="messages" showDetail="false" autoUpdate="true"
                                    closable="true" />

                                <h:inputText required="true"
                                    value="#{PageService.instance.name}"
                                    name="name" placeholder="Last Name"
                                    styleClass="lastname_new" id="lastname_new"
                                    type="text placeholder" />

                                <h:commandButton value="Press"
                                    action="#{PageService.persist()}"/>

                                    <f:ajax render="" />
                                </h:commandButton>

                            </h:form>
                        </div>

                    </div>
                </p:outputPanel>

                <p:outputPanel id="PanelThank"
                    rendered="#{PageService.isVisibilityThank()}">
                    <div>
                        Message
                    </div>
                </p:outputPanel>
            </p:outputPanel>

我真的看不出哪里失败了? ;(

【问题讨论】:

  • 对不起,我想我不够清楚。条件如何评估?你在点击一个按钮吗?评估条件后如何更新页面
  • 我根据一些假设发布了一个答案。您应该重写您的代码(正确关闭标签)并发布您正在做什么来更新您的客户端部分。
  • 好的,我可以在我的最后重现它。给我一点时间。
  • visibilityFormisVisibilityForm 的顺序排列。 Getter 不需要get/is 前缀
  • 好的,Maximus 你想要答案还是我也应该重构你的代码?

标签: jsf jsf-2 primefaces


【解决方案1】:

就像 Xtreme Biker 所说的 &lt;f:ajax&gt; render 属性将渲染 DOM 中已经存在的元素。在您的情况下,&lt;p:outputPanel id="PanelThank"&gt;render 属性正在评估为false,因此它不在 DOM 中。因此,&lt;f:ajax&gt; 无法渲染它。您需要指向始终可见的东西。改变

<f:ajax render="" />

<f:ajax render=":PanelThank" />

但更重要的是改变

<p:outputPanel id="PanelThank" rendered="#{PageService.isVisibilityThank()}">
    <div>
        Message
    </div>
</p:outputPanel>

<p:outputPanel id="PanelThank">
    <p:outputPanel rendered="#{PageService.isVisibilityThank()}">
        <div>
            Message
        </div>
    </p:outputPanel>           
</p:outputPanel>

也考虑重构您的代码。例如,不需要&lt;h:form&gt; 中的actionmethod

【讨论】:

  • 好的,谢谢你的回答!但是,现在我的持久方法出现异常:serverError: class javax.faces.el.EvaluationException Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Transaction marked as rollbackOnly。当我删除 the commandButton 中的嵌套 ajax 时,一切正常......
  • @maximus 试试这个&lt;f:ajax execute ="@form" render=":PanelThank" /&gt;,让我知道结果如何。请务必在inputText 中输入一个值。我真的不知道你的persist 在做什么,所以我不能给你一个具体的答案。如果你愿意,我可以告诉你我做了什么来测试它。我使用的是简单的布尔值而不是 JPA,但老实说,我认为您应该提出一个新问题(我不介意现在在这里帮助您,我不介意)但是您可以让更多人有机会看到您的问题并帮助您更好地理解(也许在 ajax 请求和 JPA 之间有些混乱)。
  • 是的,因为你是最好的回答者,对于 jpa 问题,我将设置一个新问题。谢谢你的帮助!!!
【解决方案2】:

rendered 属性 defines if JSF will render your component in the DOM tree。您的问题是您无法更新不在树中的组件,因为您根本没有它:

<p:outputPanel id="PanelForm" rendered="#{PageService.isVisibilityForm()}">
    //my Form
<p:outputPanel>

<!--Fails if not PageService.isVisibilityForm(), because you don't have the component itself in the tree! So can't find it-->
<p:ajax update="PanelForm"/>

您最好将您的内容包装在另一个容器中,该容器始终呈现并更新它而不是另一个容器:

<h:panelGroup id="updatablePanel">
    <p:outputPanel rendered="#{PageService.isVisibilityForm()}">
        //my Form
    <p:outputPanel>
</h:panelGroup>

<p:ajax update="updatablePanel"/>

【讨论】:

  • 感谢您的回答!我按照您在回答中的解释做了,但是,我得到&lt;p:ajax&gt; Event attribute could not be determined: null 我必须在哪个容器中提供p:ajax
  • 您使用哪个组件来更新您的视图?您提到它是您的按钮之一。您必须将 ajax 标记包装在组件中。你也可以使用f:ajax。看Ajax 'Hello World' Example
  • 我想让PanelForm消失,让PanelMessage出现。因此我更新了这两个组件。我是否必须在这两个面板中的每一个中添加一个ajax 组件?非常感谢你的回答!!!
  • 不客气,只需将&lt;f:ajax render="@form"/&gt; 输入您的按钮即可。这将使整个表单再次呈现。顺便说一句,请再次修复您的代码。
  • 我不认为 ajax 是问题所在。确实它更有效,但由于他提交了整个表单,它应该仍然可以工作并且它在我的最后(至少给出的代码)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-10-20
  • 1970-01-01
  • 2021-12-01
  • 2016-02-14
  • 1970-01-01
  • 1970-01-01
  • 2023-03-05
相关资源
最近更新 更多