Cannot create a session after the response has been committed - PrettyFaces MultiPageMessagesSupport on Glassfish4

StackOverflow https://stackoverflow.com/questions/17245979

  •  01-06-2022
  •  | 
  •  

Question

I have a Java EE app where I use JSF2 + PrettyFaces + EBJ3 + Glassfish

I just recently updated to Glassfish4 which is the default Server implementation for Java EE 7 and I started getting issues with PrettyFaces.

I have configured on my faces-config.xml:

<lifecycle>
  <phase-listener>com.ocpsoft.pretty.faces.event.MultiPageMessagesSupport</phase-listener>
</lifecycle>     

to enable for Faces Messages to be passed around correctly and displayed on screen.

But since I upgraded from Glassfish3 to 4 I started getting this issue:

java.lang.IllegalStateException: Cannot create a session after the response has been committed ...

        at com.ocpsoft.pretty.faces.util.FacesMessagesUtils.saveMessages(FacesMe
ssagesUtils.java:56)
        at com.ocpsoft.pretty.faces.event.MultiPageMessagesSupport.afterPhase(Mu
ltiPageMessagesSupport.java:66)

If I remove the listener from faces-config.xml none of the Faces messages are displayed on screen.

I am using PrettyFaces to have nice well formatted URLs to enhance SEO on my site. I wish I couldn't have to replace it or refactor my app to not use it since I already mapped a lot of the navigation flow using it. Does somebody know a better option for this scenario? I really appreciate any suggestions. Thanks.

Was it helpful?

Solution

I don't recommend to use MultiPageMessagesSupport if you are deploying to a "modern" container with JSF 2.2 support. The MultiPageMessagesSupport phase listener has been developed for JSF 1.x. JSF 2.x added support for persisting messages across redirects. Just execute this code before redirecting:

FacesContext.getCurrentInstance().getExternalContext().getFlash().setKeepMessages(true);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top