0

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 .

Stephan
  • 696
  • 15
  • 37
  • I don't see any PrimeFaces related code here – Kukeltje Oct 31 '17 at 14:50
  • You may not see, but i am using extensively Primefaces, especially in the all my pages. So my problem could be maybe in the component... – Stephan Nov 01 '17 at 06:26
  • So you tried a `h:messages` and that worked? – Kukeltje Nov 01 '17 at 06:53
  • As i mentioned, when i am in create_object.jsf and get redirected to the overview.jsf page due to a button press, then yes. If get redirected automatically from the initMyObject which is called in the create_object.jsf from a prerenderedView, then now, the message is not shown . Please let me know if i should give further details – Stephan Nov 01 '17 at 08:43
  • Read my previous comment Again please, you state in multiple places it is PrimeFaces related. So then try a plain jsf `h:messages` to confirm that – Kukeltje Nov 01 '17 at 08:56
  • Does not work h:messages either. I will try to describe my problem maybe a bit better. When i am in the create_object.jsf page and press a commandButton which adds a message in the context and redirects me to overview.jsf . In this case, the message is displayed without any problem on overview.jsf . Now the problem starts when, when i am trying to access create_object.jsf , which calls the initMyObject before the page is loaded. In this case i am redirected (see code above) to the overview.jsf page without any message to appear. – Stephan Nov 06 '17 at 14:49
  • The solution from BalusC solves my problem : https://stackoverflow.com/a/10887870/1918516 – Stephan Nov 30 '17 at 08:37
  • Good find, compliments. Great it works now. I'll try to remember this (duplicate?) Q/A – Kukeltje Nov 30 '17 at 08:43

0 Answers0