I am upgrading JSF from 1.2 to 2.2 version.
I have a simple response.sendRedirect() in my backing bean method. With JSF2.2, it started giving "java.lang.IllegalStateException: Cannot change buffer size after data has been written at org.apache.catalina.connecto" exception.
After adding "FacesContext.getCurrentInstance().responseComplete();", it worked!
Can anyone help me in understanding how the implementation is upgraded in JSF2.2 that redirect is not working without explicity saying response is completed?
Thanks!
You're supposed to use
ExternalContext#redirect()
for the job.This has always been the case since the beginning, also in JSF 1.x. It will under the covers automatically call
FacesContext#responseComplete()
after performing theHttpServletResponse#sendRedirect()
. TheresponseComplete()
will basically instruct JSF that the response is already manually completed, and that JSF basically doesn't need to proceed to render response phase then (i.e. writing the navigation outcome into the response).Moreover, any attempt to grab and downcast the raw
javax.servlet.*
API from under JSF's covers should be taken as a hint to think twice if there isn't already a JSF-ish way to achieve the same. In JSF 2.x there's an additional new way to perform a redirect: appendfaces-redirect=true
query parameter to the (implicit) navigation outcome:As to the illegal state exception you faced, JSF 2.2 just postpones setting the response headers to the point when it actually needs to render response. It will be too late if the response is already committed.