Lets say you had a page with a view param, like /widgets?widgetId=1
Use ExternalContext#responseSendError() in the Converter
when the condition is met.
context.getExternalContext().responseSendError(404, message);
context.responseComplete();
return null;
Don't forget to call FacesContext#responseComplete()
afterwards, this isn't implicitly been done for some reason, in contrary to ExternalContext#redirect()
. Otherwise JSF will append the current page to end of response, or throw an IllegalStateException
when it's already committed.
Instead of the magic number 404
you can also use HttpServletResponse.SC_NOT_FOUND.
context.getExternalContext().responseSendError(HttpServletResponse.SC_NOT_FOUND, message);
context.responseComplete();
return null;