0

Validating view-parameters in preRenderView event in the backing bean I observed that each event <f:event> is still fired even if any of the methods invoked a redirect using:

FacesContext.getExternalContext().redirect(url);

If in multiple events a redirect is invoked an illegal state error exception will be thrown.

To prevent this I would like to know if and how it is possible to generally detect wether a redirect was already invoked and either process the first or the last redirect.

djmj
  • 5,579
  • 5
  • 54
  • 92

1 Answers1

1

You can use the FacesContext#getRenderResponse() to confirm if the process (or order) to proceed to the RENDER_RESPONSE phase has been initiated. This checks whether renderResponse() method has been called by a component, signalling the runtime to transfer control to the RENDER_RESPONSE phase. Generally at this point it's unsafe to attempt to do anything with the response stream.

Another route to the RENDER_RESPONSE phase is the FacesContext#responseComplete() method. This however, does not directly trigger the response processing. Rather, it's the final flag to indicate to the runtime that RENDER_RESPONSE has been arrived at.

So, per the spec, FacesContext#getResponseComplete() is the ultimate check to verify the state of the response

In some circumstances, it is possible that both renderResponse()and responseComplete()might have been called for the request. In this case, the JSF implementation must respect the responseComplete()call (if it was made) before checking to see if renderResponse()was called.

kolossus
  • 20,559
  • 3
  • 52
  • 104
  • Thanks, that helped me to remove this error. About: `Generally at this point it's unsafe to attempt to do anything with the response stream.` So I have no chance to stop redirect to first url to redirect to second url? – djmj Sep 20 '13 at 01:09