我正在使用 JSF 和 PrimeFaces 开发应用程序,它允许在 p:dialog 中编辑 p:dataTable 行。由于我已经向输入字段添加了验证,因此在发生错误时保持对话框打开以及在对话框中替换错误消息时遇到了困难。发生了什么,在我单击 commandButton 后,对话框关闭并且消息不可见。当我重新打开对话框时,该字段是红色的,但消息仍然不可见。
<!-- Dialog box for editing row -->
<p:dialog id="editDialogWindow" header="Row Editing" widgetVar="editDialog" modal="true">
<p:panelGrid columns="5">
<p:outputLabel>Title</p:outputLabel>
<p:outputLabel>Author</p:outputLabel>
<p:outputLabel>Number Of Pages</p:outputLabel>
<p:outputLabel>Year Of Publication</p:outputLabel>
<p:outputLabel>Price</p:outputLabel>
<p:inputText value="#{libraryBean.selectedBook.title}" id="titleInput" required="True" requiredMessage="You must give the title" />
<p:inputText value="#{libraryBean.selectedBook.author}" id="authorInput" required="True" requiredMessage="You must give the author" />
<p:inputText value="#{libraryBean.selectedBook.numberOfPages}" id="numberOfPagesInput" required="True" requiredMessage="You must give the number of pages" />
<p:inputText value="#{libraryBean.selectedBook.year}" id="yearInput" required="True" requiredMessage="You must give the date of publication" >
<f:convertDateTime type="date" pattern="dd/MM/yyyy"/>
</p:inputText>
<p:inputText value="#{libraryBean.selectedBook.price}" id="priceInput" required="True" requiredMessage="You must give the price" >
<f:convertNumber maxFractionDigits="2" minFractionDigits="2" />
</p:inputText>
<p:commandButton value="Submit Changes" oncomplete="if(!args.validationFailed) {editDialog.close()}" update=":form" actionListener="#{libraryBean.incrementCounter}"/>
<p:commandButton value="Cancle" onclick="editDialog.close()" update=":form" process="@none" immediate="true"/>
</p:panelGrid>
<p:message for="titleInput" />
<p:message for="authorInput" />
<p:message for="numberOfPagesInput" />
<p:message for="yearInput" />
<p:message for="priceInput" />
</p:dialog>
编辑
我已经解决了所有问题,并且正在提供工作代码以供参考。
<!-- Dialog box for editing row -->
<p:dialog id="editDialogWindow" header="Row Editing" widgetVar="editDialog" modal="true">
<h:form id="dialogForm">
<p:panelGrid columns="5" rendered="#{!(libraryBean.selectedBook==null)}" id="panelGrid">
<p:outputLabel>Title</p:outputLabel>
<p:outputLabel>Author</p:outputLabel>
<p:outputLabel>Number Of Pages</p:outputLabel>
<p:outputLabel>Year Of Publication</p:outputLabel>
<p:outputLabel>Price</p:outputLabel>
<p:outputLabel>
<p:inputText value="#{libraryBean.selectedBook.title}" id="titleInput"
required="True" requiredMessage="You must give the title" />
<p:message for="titleInput" />
</p:outputLabel>
<p:outputLabel>
<p:inputText value="#{libraryBean.selectedBook.author}" id="authorInput"
required="True" requiredMessage="You must give the author" />
<p:message for="authorInput" />
</p:outputLabel>
<p:outputLabel>
<p:inputText value="#{libraryBean.selectedBook.numberOfPages}" id="numberOfPagesInput"
required="True" requiredMessage="You must give the number of pages"
converterMessage="This must be a number"/>
<p:message for="numberOfPagesInput" />
</p:outputLabel>
<p:outputLabel>
<p:inputText value="#{libraryBean.selectedBook.year}" id="yearInput"
required="True" requiredMessage="You must give the date of publication"
converterMessage="The date format is dd/mm/yyyy" >
<f:convertDateTime type="date" pattern="dd/MM/yyyy"/>
</p:inputText>
<p:message for="yearInput" />
</p:outputLabel>
<p:outputLabel>
<p:inputText value="#{libraryBean.selectedBook.price}" id="priceInput"
required="True" requiredMessage="You must give the price"
converterMessage="This must be a number " >
<f:convertNumber maxFractionDigits="2" minFractionDigits="2" />
</p:inputText>
<p:message for="priceInput" />
</p:outputLabel>
<p:commandButton value="Save" oncomplete="if(!args.validationFailed) editDialog.hide()" update="dialogForm, :form" actionListener="#{libraryBean.incrementCounter}"/>
<p:commandButton value="Exit" onclick="editDialog.hide()" update="dialogForm" process="@this" immediate="true">
<p:resetInput target="dialogForm:panelGrid"/>
</p:commandButton>
</p:panelGrid>
</h:form>
</p:dialog>