Tactical Catgirl
Tactical Catgirl

Reputation: 429

JSF: throw 404 error if GET parameter is omitted

I need to throw 404 error, if invalid GET parameter is passed to page. I've attached it to validator as described here. But if there is no parameter at all, validator is not being invoked. How can I handle this situation?

Upvotes: 2

Views: 1153

Answers (1)

Fritz
Fritz

Reputation: 10045

You can place exactly the same check you're doing now with the validator, but inside a listener associated to the preRenderView event:

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

This validateParams listener should have a check like this one:

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
}

This approach works for multiple parameters, where you can validate each one of them and act accordingly.

Upvotes: 1

Related Questions