How to send a person to a 404 page if f:viewParam / converter returns null?

做~自己de王妃 提交于 2019-12-02 01:11:48

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;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!