我正在使用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>