如果将无效的GET参数传递给页面,则需要抛出404错误.我已按照此处所述将其附加到验证程序.但是,如果根本没有任何参数,则不会调用验证器.我该如何处理这种情况?
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?
推荐答案您可以使用验证器放置与您现在所做的检查完全相同的检查,但可以将其放置在与preRenderView事件关联的侦听器中:
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"/>此validateParams侦听器应具有如下检查:
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.
更多推荐
JSF:如果省略GET参数,则抛出404错误
发布评论