JSF:如果省略GET参数,则抛出404错误

编程入门 行业动态 更新时间:2024-10-10 12:26:46
本文介绍了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错误

本文发布于:2023-11-28 17:04:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1643244.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:抛出   错误   参数   JSF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!