Search code examples
http-redirectjsf-2java-ee-6omnifaces

JSF - Keep Faces Messages after redirect from @PostConstruct


I have page1 with button that navigates to page2, page 2 add some messages and navigates back to page1. I want display those messages on page1. I've tried many solutions, but nothing work.

Sample code page1.xhtml:

<p:commandButton value="edit" action="#{bean1.edit}"/>

In the managed bean:

public String edit() {
  return "page2?faces-redirect=true";
}

page2 managed bean

@PostConstruct
private void postConstruct() {
  Faces.getFlash().setKeepMessages(true);
  Messages.addFlashGlobalError("cannot edit!");
  Faces.navigate("page1?faces-redirect=true");
}

Both beans are view scoped and both pages have <p:messages> at the end of body.


Solution

  • First of all, I'm not entirely sure that the @PostConstruct is the best place to perform a redirect. See this. That being said, google turned up this and it looks reasonable. Try redirecting within the facelets page itself with a preRender event tag close to the top of the page. Cheers