2

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?

Community
  • 1
  • 1
Tactical Catgirl
  • 419
  • 4
  • 10
  • In the validate method (in the post linked by you), you can still find out if the parameter received is `null` and still redirect. Did you try this? – Vikas V Feb 25 '13 at 13:59
  • Yes, I tried. As I said, validate method is invoked only if parameter is received. I think, URL should look like `/page.jsf?param=` to pass `null` to it. – Tactical Catgirl Feb 25 '13 at 14:17

1 Answers1

1

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.

Fritz
  • 9,987
  • 4
  • 30
  • 49