简体   繁体   中英

Rerender RichFaces error message after ajax request

I am using custom ajax-called javacode that does some processing on the server. In this process various errors can occure that I add to the FacesContext via addMessage() . I want to display these messages in the same <rich:messages> -tag that I use for my validation errors.

Do you know a way to display these messages in the <rich:messages> -tag after the ajax-request completed?

My initial idea was adding <a4j:jsFunction name="richDisplayError" reRender="messages" /> to the markup and calling richDisplayError when the request completed, but it seems the messages panel is rerendered empty.

<rich:messages> has ajaxRendered set to true by default. So the problem lies elsewhere. Perhaps:

  • you are redirecting, instead of forwarding, and the messages are lost
  • you aren't actually adding the messages (check with debug)
  • you are having different/lacking views/subviews

For example, in your page:

    <a4j:commandButton value="Action"
           limitToList="true" 
           action="#{mybean.action}"
           reRender="mymessages">
    </a4j:commandButton>
    <a4j:outputPanel ajaxRendered="true">
       <h:messages id="mymessages"  />
    </a4j:outputPanel>

then in you bean:

public void action(){                         
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("hello world")); 
}    

You need 3 things :

1st : declare your error message, in the "\\resources\\bundle\\errorMessages.properties" file, like this :

errorMsgToDisplay.errName = Your Error Message Here

2nd : declare your BUNDLE variable in the class code :

private static final ResourceBundle BUNDLE = ResourceBundle.getBundle("/bundle/errorMessages");

3rd : Display the message (after an condition for example )

if  ( condition ) {
FacesContext.getCurrentInstance().addMessage("", new FacesMessage(FacesMessage.SEVERITY_ERROR, 
BUNDLE.getString("errorMsgToDisplay.errName"),
BUNDLE.getString("errorMsgToDisplay.errName")));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM