问题
i have a strange problem with a p:message tag. On page a have a dataTable with data from my db that i can edit in a p:dialog. Once all validations succeds and the update in db is done i refresh the dataTable and add a faces message info to display the succes of the operation. In the front-end i update the form whom contains both the dataTable and the dialog. My problem is that the message is displayed but almost instantly disapears. It's like the message tag is updated with the form. I don't get it.
I have tried to move the message tag in & out of the form in didn't change anything. I have tried to tweak the remoteCommand to update just the dataTable and the dialog but it didn't work.
<p:messages autoUpdate="true" showDetail="true" severity="info,error" />
<h:form id="form">
<p:dataTable
style="width: 80%; margin-left: auto; margin-right: auto; text-align:center"
var="achievement" value="#{achievementBean.listAchievement}">
...
</p:dataTable>
<p:dialog header="#{i18n['achievement']}" widgetVar="dlg"
dynamic="true" closable="false" resizable="false" showEffect="fade"
hideEffect="fade">
<h:panelGroup id="achievementDetail">
<p:messages autoUpdate="true" severity="warn" />
...
<h:panelGrid columns="2" style="width: 100%; text-align:center">
<p:commandButton value="#{i18n['general.submit']}"
icon="fa fa-check"
actionListener="#{achievementBean.submitAchievement}"
oncomplete="if(!args.validationFailed){updateForm();}" />
<p:commandButton value="#{i18n['general.cancel']}"
icon="fa fa-close" action="#{achievementBean.submitCancel}"
oncomplete="PF('dlg').hide();" update="@form" process="@this" />
</h:panelGrid>
<p:remoteCommand name="updateForm" update="@form" />
</h:panelGroup>
</p:dialog>
</h:form>
回答1:
Your concrete problem boils down to this:
<p:messages autoUpdate="true" />
<h:form>
<p:dataTable ...>
...
</p:dataTable>
...
<p:dialog>
...
<p:commandButton
action="#{achievementBean.submitAchievement}"
oncomplete="if(!args.validationFailed){updateForm();}" />
<p:remoteCommand name="updateForm" update="@form" />
</p:dialog>
</h:form>
- The
autoUpdate="true"
will automatically update the component on every ajax request. - The
<p:commandButton>
invokes an ajax request which adds a message and invokes theupdateForm()
remote command. The message is displayed on that ajax request. - The
<p:remoteCommand>
invokes another ajax request. But this one doesn't add any messages, so nothing is displayed on that ajax request. In effects, the message displayed in the previous ajax request gets cleared out.
You can use ignoreAutoUpdate
attribute of the command component in order to let it ignore any autoUpdate
-able component. So your solution is:
<p:remoteCommand name="updateForm" update="@form" ignoreAutoUpdate="true" />
That said, why do you still have both the <p:dialog>
and the <p:dataTable>
in the same <h:form>
? It appears that you didn't take or understood the recommendation in my answer to your previous question p:commandButton doesn't dislpay p:dialog. Therein I recommended to move the <p:dialog>
outside the <h:form>
and give it its own <h:form>
. It makes these things so much easier manageable.
You should always give the <p:dialog>
its own <h:form>
.
<p:messages autoUpdate="true" />
<h:form>
<p:dataTable ...>
...
</p:dataTable>
<p:remoteCommand name="updateForm" update="@form" ignoreAutoUpdate="true" />
</h:form>
<p:dialog widgetVar="dlg">
<h:form>
...
<p:commandButton
action="#{achievementBean.submitAchievement}"
update="@form"
oncomplete="if(!args.validationFailed){PF('dlg').hide();updateForm();}" />
</h:form>
</p:dialog>
See also:
- How to use <h:form> in JSF page? Single form? Multiple forms? Nested forms?
回答2:
Thanks for yours answers. Sorry about the mre i always forget about it. I'll try to not forget it next time. I finally managed to solve my problem.
- I added an id to the first
<p:message>
- I added an update of this id in the submit
<p:commandButton>
来源:https://stackoverflow.com/questions/56328646/pmessages-displays-but-disapear-almost-instantly