【发布时间】:2016-01-01 22:28:24
【问题描述】:
我想在我的页面上显示每个操作的响应消息。 当更新用户并且操作成功时,我想显示一条可关闭的消息:
“用户成功更新”如果操作不成功我想显示我在服务器端遇到的错误或异常。
我已尝试使用此处的示例: http://www.primefaces.org/showcase/ui/message/messages.xhtml
几个问题:
- 消息不可关闭
- 所有消息同时显示,我无法为消息附加密钥,即使我为每条消息使用了不同的“for”属性。
我正在合作:
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>3.1.1</version>
</dependency>
xhtml 页面:
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<h:outputStylesheet library="css" name="table-style.css" />
</h:head>
<h:form id="ni">
<p:messages for="Message1" showDetail="true" autoUpdate="true"
closable="true" globalOnly="false" />
<p:messages for="Message2" showDetail="true" autoUpdate="true"
closable="true" globalOnly="false" />
<h:dataTable value="#{normalIdentifiers}" var="i"
styleClass="order-table" headerClass="order-table-header"
rowClasses="order-table-odd-row,order-table-even-row"
columnClasses="order-column,order-column,order-column,order-column,order-column,order-column,
order-column,order-column,order-column,order-column,order-column,order-column">
<h:column>
<f:facet name="header">User Id</f:facet>#{i.userId}</h:column>
..
<h:column>
<f:facet name="header"></f:facet>
<p:commandButton id="niUpdate#{i.id}" value="Update"
onclick="niConfirmationUpdate#{i.id}.show()" type="button" />
<p:confirmDialog id="niUpdateConfirmDialog#{i.id}"
message="Are you sure you want to Update this Identifier?"
showEffect="drop" hideEffect="drop" header="Update Identifier"
severity="alert" widgetVar="niConfirmationUpdate#{i.id}"
modal="true">
<h:commandButton value="Yes"
oncomplete="niConfirmationUpdate#{i.id}.hide()"
actionListener="#{identifierManager.updateNormalIdentifier(i)}"
style="width: 80px;height: 24px;margin: 10px 5px;" />
<h:commandButton value="No"
onclick="niConfirmationUpdate#{i.id}.hide()" type="button"
style="width: 80px;height: 24px" />
</p:confirmDialog>
</h:column>
<h:column>
<f:facet name="header"></f:facet>
<p:commandButton id="niDelete#{i.id}" value="Delete"
onclick="niConfirmationDelete#{i.id}.show()" type="button" />
<p:confirmDialog id="niDeleteConfirmDialog#{i.id}"
message="Are you sure you want to delete this Identifier?"
showEffect="drop" hideEffect="drop" header="Delete"
severity="alert" widgetVar="niConfirmationDelete#{i.id}"
modal="true">
<h:commandButton value="Yes"
oncomplete="niConfirmationDelete#{i.id}.hide()"
actionListener="#{identifierManager.removeNormalIdentifier(i)}"
style="width: 80px;height: 24px;margin: 10px 5px;" />
<h:commandButton value="No"
onclick="niConfirmationDelete#{i.id}.hide()" type="button"
style="width: 80px;height: 24px" />
</p:confirmDialog>
</h:column>
<h:column>
<f:facet name="insert"></f:facet>
<p:commandButton type="button" value="Insert" id="niInsert#{i.id}"
onclick="niInsert#{i.id}.show()" ajax="false">
</p:commandButton>
<p:dialog id="niConfirmDialogInsert#{i.id}"
header="Insert New Identifier" widgetVar="niInsert#{i.id}"
height="300" width="300">
<h:panelGrid columns="2" style="vertical-align:top; ">
..
</h:panelGrid>
<h:commandButton id="niConfirmInsert#{i.id}" value="OK"
oncomplete="niInsert#{i.id}.hide()"
action="#{identifierManager.insertNormalIdentifier(i.userId)}"
style="width: 80px;height: 24px;margin: 10px 5px;" />
<h:commandButton id="niDeclineInsert#{i.id}" value="Cancel"
onclick="niInsert#{i.id}.hide()" type="button"
style="width: 80px;height: 24px" />
</p:dialog>
</h:column>
</h:dataTable>
</h:form>
</ui:composition>
豆子:
@ManagedBean(name = "identifierManager")
@SessionScoped
public class IdentifiersManager {
...
public void updateNormalIdentifier(NormalIdentifier normalIdentifier) {
...
FacesContext.getCurrentInstance().addMessage("Message1", new FacesMessage(FacesMessage.SEVERITY_INFO, "Update", "Update"));
}
public void insertNormalIdentifier(Integer userId) {
try {
..
FacesContext.getCurrentInstance().addMessage("Message2", new FacesMessage(FacesMessage.SEVERITY_ERROR, "Insert", "Insert"));
} catch (Exception e) {
logger.error(e, e);
}
}
public void removeNormalIdentifier(NormalIdentifier identifier) {
...
}
}
我使用过“p:message”,但现在,我如何在几秒钟后隐藏消息。另外,当页面重新加载时,我如何清除所有消息。 这是更新的代码:
xhtml 页面:
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<h:outputStylesheet library="css" name="table-style.css" />
</h:head>
<h:form id="ni">
<h:inputHidden id="txt" value="1" />
<p:message for="txt" />
<h:dataTable value="#{normalIdentifiers}" var="i"
styleClass="order-table" headerClass="order-table-header"
rowClasses="order-table-odd-row,order-table-even-row"
columnClasses="order-column,order-column,order-column,order-column,order-column,order-column,
order-column,order-column,order-column,order-column,order-column,order-column">
<h:column>
<f:facet name="header">User Id</f:facet>#{i.userId}</h:column>
..
<h:column>
<f:facet name="header"></f:facet>
<p:commandButton id="niUpdate#{i.id}" value="Update"
onclick="niConfirmationUpdate#{i.id}.show()" type="button" />
<p:confirmDialog id="niUpdateConfirmDialog#{i.id}"
message="Are you sure you want to Update this Identifier?"
showEffect="drop" hideEffect="drop" header="Update Identifier"
severity="alert" widgetVar="niConfirmationUpdate#{i.id}"
modal="true">
<h:commandButton value="Yes"
oncomplete="niConfirmationUpdate#{i.id}.hide()"
actionListener="#{identifierManager.updateNormalIdentifier(i)}"
style="width: 80px;height: 24px;margin: 10px 5px;" />
<h:commandButton value="No"
onclick="niConfirmationUpdate#{i.id}.hide()" type="button"
style="width: 80px;height: 24px" />
</p:confirmDialog>
</h:column>
<h:column>
<f:facet name="header"></f:facet>
<p:commandButton id="niDelete#{i.id}" value="Delete"
onclick="niConfirmationDelete#{i.id}.show()" type="button" />
<p:confirmDialog id="niDeleteConfirmDialog#{i.id}"
message="Are you sure you want to delete this Identifier?"
showEffect="drop" hideEffect="drop" header="Delete"
severity="alert" widgetVar="niConfirmationDelete#{i.id}"
modal="true">
<h:commandButton value="Yes"
oncomplete="niConfirmationDelete#{i.id}.hide()"
actionListener="#{identifierManager.removeNormalIdentifier(i)}"
style="width: 80px;height: 24px;margin: 10px 5px;" />
<h:commandButton value="No"
onclick="niConfirmationDelete#{i.id}.hide()" type="button"
style="width: 80px;height: 24px" />
</p:confirmDialog>
</h:column>
<h:column>
<f:facet name="insert"></f:facet>
<p:commandButton type="button" value="Insert" id="niInsert#{i.id}"
onclick="niInsert#{i.id}.show()" ajax="false">
</p:commandButton>
<p:dialog id="niConfirmDialogInsert#{i.id}"
header="Insert New Identifier" widgetVar="niInsert#{i.id}"
height="300" width="300">
<h:panelGrid columns="2" style="vertical-align:top; ">
..
</h:panelGrid>
<h:commandButton id="niConfirmInsert#{i.id}" value="OK"
oncomplete="niInsert#{i.id}.hide()"
action="#{identifierManager.insertNormalIdentifier(i.userId)}"
style="width: 80px;height: 24px;margin: 10px 5px;" />
<h:commandButton id="niDeclineInsert#{i.id}" value="Cancel"
onclick="niInsert#{i.id}.hide()" type="button"
style="width: 80px;height: 24px" />
</p:dialog>
</h:column>
</h:dataTable>
</h:form>
</ui:composition>
豆子:
@ManagedBean(name = "identifierManager")
@SessionScoped
public class IdentifiersManager {
...
public void updateNormalIdentifier(NormalIdentifier normalIdentifier) {
...
FacesContext.getCurrentInstance().addMessage("normalUserIdentifiers:ni:txt", new FacesMessage(FacesMessage.SEVERITY_INFO, "Info", "PrimeFaces Rocks."));
}
public void insertNormalIdentifier(Integer userId) {
try {
..
} catch (Exception e) {
logger.error(e, e);
}
}
public void removeNormalIdentifier(NormalIdentifier identifier) {
...
}
}
【问题讨论】:
-
您可以在每个阶段使用一些可以通用的通知机制来发送不同类型的通知。想到了 Cometd,您可以使用它在每个步骤中向客户端发送消息。
-
您尝试在 3.1.1 上使用 5.2.x 展示示例...不好
标签: spring jsf-2 primefaces