JSF:如果省略GET参数,则抛出404错误 [英] JSF: throw 404 error if GET parameter is omitted

查看:90
本文介绍了JSF:如果省略GET参数,则抛出404错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果将无效的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错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆