This can happen if @PostConstruct is called too late. Apparently, the bean is referenced and thus constructed for the first time relatively "late" in the view (for example, at the very bottom). At this point, the answer may already be fixed, which is the point of no return. You can no longer switch to another species.
Basically you want to call the init() method before rendering the answer. In OmniFaces, you can use the following approach in page2.xhtml :
<f:metadata> <f:viewParam name="dummy" /> <f:event type="postInvokeAction" listener="#{bean.init}" /> </f:metadata>
(you can remove <f:viewParam name="dummy" /> if you already have your own viewing options on this page, just to complete the INVOKE_ACTION phase, see also the postInvokeAction demo page )
and just a simple <f:event listener> method:
public void init() { Messages.addFlashGlobalError("cannot edit!"); Faces.navigate("page1?faces-redirect=true");
Faces.getFlash().setKeepMessages(true); not required since Messages#addFlashGlobalError() already does this. Keep in mind that in Mojarra, the Flash area will not work if navigation is specified in a different folder in the URL. Both pages must be in the same folder in the URL. This is recorded in the upcoming Mojarra 2.1.14.
source share