Get error messages

2019-04-13 02:10发布

We are using the following code for adding new error messages while validating the document:

function addFacesMessage( message, component ){
     try {
        if( typeof component === 'string' ){
            component = getComponent( component );
        }

        var clientId = null;
        if( component ){
            clientId = component.getClientId( facesContext );
        }

        facesContext.addMessage( clientId, new javax.faces.application.FacesMessage( message ) );
     } catch(e){ 
        globalScriptErrors.add(e);
        requestScope.put("scriptErrors", globalScriptErrors);
     }
}

We call this function in every validation routine, if an error occured:

facesContext.addMessage("", 
            new javax.faces.application.FacesMessage("errormessage" );

In our XPage we've got an error message box to show all errors, that have occured for the current page:

<xp:messages id="messages2" styleClass="lotusMessage lotusWarning"></xp:messages>

Now, messages are displayed in the error message box, but how can we check if there are errors for this page? We want to use this information e.g. for an popup, that only has to be displayed, if no errors are displayed in the error message box. But how do we get this information?

2条回答
Deceive 欺骗
2楼-- · 2019-04-13 02:37

Use the following to check for messages:

facesContext.getMessages().hasNext()

It will return true if messages exist, and false if no messages exist.

You can use this to control the rendering of e.g. a div like this:

<xp:div rendered="#{javascript:facesContext.getMessages().hasNext()}">
</xp:div>
查看更多
爷的心禁止访问
3楼-- · 2019-04-13 02:41

It should work with:

if (facesContext.getMessages().hasNext())

Here a working example:

<xp:text escape="true" id="computedField1">
<xp:this.value><![CDATA[#{javascript:if (facesContext.getMessages().hasNext())
        return "there is a error message";
    else
        return "no message";}]]></xp:this.value>
</xp:text>

<xp:button value="no title" id="button1">
    <xp:eventHandler event="onclick" submit="true" refreshMode="complete">
            <xp:this.action><![CDATA[#{javascript:
var message = "test";
var component = "none"

        try {
                if( typeof component === 'string' ){
                    component = getComponent( component );
                }


            var clientId = null;
            if( component ){
                clientId = component.getClientId( facesContext );
            }

            facesContext.addMessage( clientId, new javax.faces.application.FacesMessage( message ) );
         } catch(e){ 
            globalScriptErrors.add(e);
            requestScope.put("scriptErrors", globalScriptErrors);
         }
    }]]></xp:this.action>
  </xp:eventHandler>

查看更多
登录 后发表回答