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

前端 未结 1 1231
臣服心动
臣服心动 2021-01-22 18:49

Lets say you had a page with a view param, like /widgets?widgetId=1

    
        

        
相关标签:
1条回答
  • 2021-01-22 19:18

    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;
    
    0 讨论(0)
提交回复
热议问题