When visiting a page (lets call the page create_object.jsf), a preRenderView calls an init method in my Bean. In that init method of the Bean i want to immediately redirect to another page while showing also a message.
Below is my code.
public void initMyObject(){
if(condition...){
ExternalContext externalContext = context.getExternalContext();
context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"My personal message", StringUtils.EMPTY));
externalContext.getFlash().setKeepMessages(true);
externalContext.redirect(new StringBuilder(externalContext.getRequestContextPath())
.append("/overview.jsf?faces-redirect=true").toString());
return;
}
}
My jsf page on which i want the message to appear, contains the following component.
<p:messages id="messages" autoUpdate="true" />
This does indeed redirect me to the overview page, but no message appears. On the other side, when the create_object.jsf is loaded and press a commandButton to redirect me on the overview.jsf and show a message , this does work without a problem. Note, i am using in this case the very same code as a bove in a redirect method. So my guess is that the message is lost due to the fact that before it loads the create_object.jsf it is being redirected to another page.
Note: I just confirmed, that when i am on page create_object.jsf and call with a commandButton a method that redirects me to overview.jsf while adding also a message in the context, then the message does appear on overview.jsf .
Now,in another scenario, when try to get to the create_object.jsf page, the page calls the initMyObject which automatically redirects me to the overview.jsf page while adding a message in the context. In that case the message is not shown on the overview.jsf .