显示每个操作的成功/失败响应

时间:2015-10-05 09:52:14

标签: spring jsf-2 primefaces

我想在我的页面上显示每个操作的响应消息。 更新用户并且操作成功时,我想显示一个可关闭的消息:

"用户已成功更新"如果操作不成功,我想显示我在服务器端获得的错误或异常。

我尝试过使用这里的示例: {{3}}

几个问题:

  1. 消息不可关闭
  2. 所有消息都是同时显示的,即使我使用了不同的"用于消息,我也无法将密钥附加到消息上。每条消息的属性。
  3. 我正在使用:

    <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) {
          ...
        }
    }
    

    我已经使用了&#34; p:message&#34;,但现在,我怎样才能在几秒钟后隐藏消息。另外,如何在页面重新加载时清除所有消息。 这是更新的代码:

    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) {
          ...
        }
    }
    

1 个答案:

答案 0 :(得分:0)

如果您想使用<p:message属性,则应使用<p:messages代替for

不幸的是,closable属性仅适用于更新版本的<p:messages元素。

有关详细信息,请参阅:http://primefaces.googlecode.com/files/primefaces_users_guide_3_1.pdf