gpt4 book ai didi

jsf - Primefaces p :messages won't show first FacesMessage

转载 作者:行者123 更新时间:2023-12-02 17:43:36 25 4
gpt4 key购买 nike

先决条件:
- JSF 2.1
- Primefaces 5.2
- Glassfish 3.1

故事:
我创建了一个 p:dialog,用于在 p:messages 元素上显示 FacesMessages。需要此对话框,因为用户必须在继续之前使用“确定”按钮提交特定的 FacesMessage。

对话框:

<p:outputPanel id="modalMessage">

<p:dialog id="dlgMessageDialog" dynamic="true" style="z-index: 100"
closable="false" widgetVar="wigVarMessageDialog" modal="true"
appendTo="@(body)">

<f:facet name="header">
<h:outputText id="messageDialogHeader"
value="#{messageDialogBean.header}" />
</f:facet>

<p:outputPanel id="modalMessagePanel">

<h:form id="messageForm" enctype="multipart/form-data">

<p:messages id="messages" escape="false" closable="false"
showDetail="true" autoUpdate="true"
for="#{messageDialogBean.messageDialogId}"></p:messages>

<p:spacer height="20px"></p:spacer>

<p:commandButton value="#{msg.btnOk}"
oncomplete="PF('wigVarMessageDialog').hide()" />

</h:form>
</p:outputPanel>
</p:dialog>
</p:outputPanel>

Bean:

@Named("messageDialogBean")
@SessionScoped
public class MessageDialogBean implements Serializable {

private static final long serialVersionUID = 1L;
private final String messageDialogId = "messageDialogId";
private FacesMessage message = new FacesMessage();
private String header = "test";

public void showMessage(final String pHeader, final FacesMessage pMessage) {
if (pMessage != null) {
setHeader(pHeader);
this.message = pMessage;
show();
}
}

public void showWarn(final String pHeader, final String pSummary, final String pDetail) {
setHeader(pHeader);
this.message = new FacesMessage(FacesMessage.SEVERITY_WARN, pSummary, pDetail);
show();
}

public void showInfo(final String pHeader, final String pSummary, final String pDetail) {
setHeader(pHeader);
this.message = new FacesMessage(FacesMessage.SEVERITY_INFO, pSummary, pDetail);
show();
}

public void showError(final String pHeader, final String pSummary, final String pDetail) {
setHeader(pHeader);
this.message = new FacesMessage(FacesMessage.SEVERITY_ERROR, pSummary, pDetail);
show();
}

public void updateDialog() {
RequestContext context = RequestContext.getCurrentInstance();
context.update("mainForm:messageDialogHeader");
}

private void show() {
updateDialog();
RequestContext context = RequestContext.getCurrentInstance();
context.execute("PF('wigVarMessageDialog').show();");
FacesContext.getCurrentInstance().addMessage(this.messageDialogId, this.message);
}

public String getMessageDialogId() {
return this.messageDialogId;
}

public void setHeader(final String pHeader) {
this.header = pHeader;
}

public String getHeader() {
return this.header;
}

public FacesMessage getLastMessage() {
return this.message;
}

}

必须提交的消息之一:

this.messageDialogBean.showInfo("Title", "Summary", "Detail");

问题:
第一次打开对话框时,对话框的 p:messages 元素不显示消息。打开并隐藏它后,它会显示所有其他 FacesMessages 就好了。

问题:
到目前为止,我正在使用在界面初始化时打开和关闭对话框一次作为解决方法。有谁知道导致这个问题的原因以及如何正确解决它?

谢谢各位的解答

最佳答案

首先,不允许将一个表单放入另一个表单中,正如 W3C XHTML 规范中所述,“表单不得包含其他表单元素”。访问:https://www.w3.org/TR/xhtml1/#prohibitions .

所以你的对话框不应该在主窗体内,你必须将对话框从窗体中分离出来,你的代码应该像这样组织:

<form id="mainForm" >
<!--your main page-->
</form>

<p:dialog id="dlgMessageDialog" >
<h:form id="messageForm" enctype="multipart/form-data">
<f:facet name="header">
<h:outputText id="messageDialogHeader"
value="#{messageDialogBean.header}" />
</f:facet>

<p:messages id="messages" escape="false" closable="false"
showDetail="true" autoUpdate="true"
for="#{messageDialogBean.messageDialogId}"></p:messages>

<p:spacer height="20px"></p:spacer>

<p:commandButton value="#{msg.btnOk}"
oncomplete="PF('wigVarMessageDialog').hide()" />

</h:form>
</p:dialog>

另一件事,您必须更新整个对话框,因此当打开对话框时,消息会自动更新:

context.update("dlgMessageDialog");

关于jsf - Primefaces p :messages won't show first FacesMessage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39895390/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com