2

如果将无效的 GET 参数传递给页面,我需要抛出 404 错误。我已将其附加到验证器,如此处所述。但是如果根本没有参数,验证器就不会被调用。我该如何处理这种情况?

4

1 回答 1

1

您可以使用验证器进行与现在完全相同的检查,但在与preRenderView事件关联的侦听器中:

<f:event listener="#{yourBean.validateParams}" type="preRenderView"/>

这个validateParams监听器应该有一个像这样的检查:

public void validateParams() {
    if (yourParam == null || /*Other fitting conditions here*/) {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        facesContext.getExternalContext().responseSendError(404, "The param 'yourParam' is missing");
        facesContext.responseComplete();
    }
    //Other params here
}

这种方法适用于多个参数,您可以在其中验证每个参数并采取相应措施。

于 2013-02-25T13:53:15.100 回答