简体   繁体   English

Ajax在jsf视图中不起作用

[英]Ajax not working in jsf view

In a JSF Primefaces / PrimefacesExtension app, i have some ajax misbehaviour. 在JSF Primefaces / PrimefacesExtension应用程序中,我有一些ajax异常行为。

There is a section of the view, only shown when a condition is true: 视图的一部分,仅在条件为true时显示:

<h:form id="membership" rendered="#{empty registerVisitBean.partner.activeMembership.payDate}">

Inside, there are 2 grids: first one with red text and a button to make action (pay a membership), this first grid will dissapear when action done, and a second one will be shown: 内部有2个网格:第一个网格带有红色文本和一个用于执行操作(支付会员资格)的按钮,该第一个网格将在操作完成后消失,第二个网格将显示:

    <p:panelGrid id="not_paid" layout="grid" columns="2" rendered="#{!registerVisitBean.paid}">
        <h:outputText value="No pagada" styleClass="data-text red"  />
        <p:commandButton value="Pagar" actionListener="#{registerVisitBean.payMembership}" update="paid,not_paid" />
    </p:panelGrid>

    <p:panelGrid id="paid" layout="grid" columns="1" rendered="#{registerVisitBean.paid}">
        <h:outputText value="#{registerVisitBean.partner.activeMembership.payDate}" styleClass="data-text" />
    </p:panelGrid>

The view is quite long, but other parts have independent forms and are not important here... The <form> tag is NOT nested inside other <form> but it is in other <p:panelGrid> , but not a problem i think, this is happening in same view to change the color of an element after making action and works perfect: 该视图很长,但其他部分具有独立的形式,在这里并不重要... <form>标记未嵌套在其他<form>但在其他<p:panelGrid> ,但我认为不是问题,这是在同一视图中发生的,以在执行操作并完美运行后更改元素的颜色:

<h:form id="notes">
<p:panelGrid id="partner_notes" layout="grid" columns="1" >
    <p:inputTextarea value="#{registerVisitBean.partnerNotes}" id="notes" rows="2" autoResize="false" styleClass="#{registerVisitBean.saved ? 'full-width unresizable success' : 'full-width unresizable'}" style="padding: 0"></p:inputTextarea>
    <p:commandButton value="Guardar Notes" actionListener="#{registerVisitBean.updateNotes}" styleClass="full-width" update=":notes:partner_notes"/>
</p:panelGrid>
</h:form>

I think problem is in the update part of the <p:commandButton> but i've tried several ways without succeed. 我认为问题出在<p:commandButton>update部分,但是我尝试了几种方法而没有成功。

NOTE: bean code is being executed: 注意:正在执行bean代码:

private boolean paid; 
// ( getter / setter)

public void payMembership(){
    MembershipDto membership =  partner.getActiveMembership();
    membership.setPayDate(new Date());
    membership.setMember(partner);
    membership = partnerManagementFacade.updateMembership(membership);
    if (membership != null){
        System.err.println("Paid > " + membership.getPayDate());
        paid = true;
    }
}

With java code there is no problem, debugging shows all goes correct, but after the view is not refreshed. 使用Java代码没有问题,调试显示一切正常,但是视图未刷新之后。

EDIT1: according to @stg answer, i tried unsuccesfully: EDIT1:根据@stg答案,我尝试失败:

<p:outputPanel id="container">
<p:panelGrid id="not_paid" layout="grid" columns="2" rendered="#{!registerVisitBean.paid}">
    <h:outputText value="No pagada" styleClass="data-text red"  />
    <p:commandButton value="Pagar" actionListener="#{registerVisitBean.payMembership}" update="container" />
</p:panelGrid>

<p:panelGrid id="paid" layout="grid" columns="1" rendered="#{registerVisitBean.paid}">
    <h:outputText value="#{registerVisitBean.partner.activeMembership.payDate}" styleClass="data-text" />
</p:panelGrid>

As his answer says: 正如他的回答所说:

You can not update a component, that has not been rendered previously, because it is "just not there"! 您无法更新以前未渲染的组件,因为它“不存在”!

I think with his code, the problem will persist (second panel grid is not being rendered first time, so I cannot update update it cause it doesn't exists. 我认为使用他的代码,问题将继续存在(第二个面板网格未在第一次呈现,因此我无法更新更新,因为它不存在。

Trying to avoid this problem i decided to update the <p:outputText> and remove the <p:commandButton> from the view: 为了避免此问题,我决定更新<p:outputText>并从视图中删除<p:commandButton>

<h:form id="membership">
    <h:panelGroup id="not_paid" layout="block" rendered="#{empty registerVisitBean.partner.activeMembership.payDate}">
        <h:outputText value="#{registerVisitBean.paid ? registerVisitBean.partner.activeMembership.payDate : 'No pagada'}" styleClass="data-text red"  />
        <p:commandButton value="Pagar" actionListener="#{registerVisitBean.payMembership}" update="not_paid,@this" rendered="#{!registerVisitBean.paid}"/>
    </h:panelGroup>
</h:form>

Instead of expected... this is not working either. 出乎意料之外……这也不起作用。 :'( :'(

Any ideas? 有任何想法吗?

You can not repaint a wall you never built! 您无法重涂从未建造过的墙!

Here: You can not update a component, that has not been rendered previously, because it is "just not there"! 此处:您无法更新以前未渲染的组件,因为它“不存在”!

What you may do is nest your panelgrid in a surrounding outputpanel and update this outputpanel instead of the grids: 您可以做的是将面板网格嵌套在周围的输出面板中,并更新此输出面板而不是网格:

<p:outputPanel id="container">
    <p:panelGrid id="not_paid" layout="grid" columns="2" rendered="#{!registerVisitBean.paid}">
        <h:outputText value="No pagada" styleClass="data-text red"  />
        <p:commandButton value="Pagar" actionListener="#{registerVisitBean.payMembership}" update="container" />
    </p:panelGrid>

    <p:panelGrid id="paid" layout="grid" columns="1" rendered="#{registerVisitBean.paid}">
        <h:outputText value="#{registerVisitBean.partner.activeMembership.payDate}" styleClass="data-text" />
    </p:panelGrid>
</p:outputPanel>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM