如果f:viewParam为空,则重定向 [英] Redirect if a f:viewParam is empty

查看:408
本文介绍了如果f:viewParam为空,则重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Сan如果 f:viewParam 为空,我做一个重定向(或错误)

Сan I do a redirect (or error) if a f:viewParam is empty?

<f:metadata>
    <f:viewParam name="accountId" value="#{accountMB.id}"/>
</f:metadata>

当我添加 required =true , 什么都没发生。有什么选择?

When I add required="true", nothing happens. What are the options?

推荐答案


当我添加 =true,没有任何反应

When I add required="true", nothing happens

你需要 ; h:message(s)> 显示与给定(输入)组件相关联的面部消息。您可能已经知道如何执行< h:inputText> 。您可以为< f:viewParam> 完全一样。

You need <h:message(s)> to show faces messages associated with a given (input) component. You probably already know how to do that for <h:inputText>. You can do exactly the same for <f:viewParam>.

<f:metadata>
    <f:viewParam id="foo" ... required="true" />
</f:metadata>
...
<h:message for="foo" />








Сan如果一个 f:viewParam 是空的,我做一个重定向(或错误)

Сan I do a redirect (or error) if a f:viewParam is empty?

不直接使用标准的JSF验证工具。您需要在< f:viewAction> 中手动执行作业(您需要确保您没有任何验证器/转换器,否则它不会因验证/转换错误而被调用;您也可以使用< f:event type =preRenderView> )。

Not directly with standard JSF validation facilities. You'd need to do the job manually in <f:viewAction> (you need to make sure that you don't have any validators/converters on it, otherwise it wouldn't be invoked due to a validation/conversion error; you could alternatively use <f:event type="preRenderView">).

<f:metadata>
    <f:viewParam value="#{bean.foo}" />
    <f:viewAction action="#{bean.checkFoo}" />
</f:metadata>

public String checkFoo() {
    if (foo == null || foo.isEmpty()) {
        return "some.xhtml"; // Redirect to that page.
    } else {
        return null; // Stay on current page.
    }
}

发送HTTP错误可以如下完成(这示例发送HTTP 400错误):

Sending a HTTP error can be done as below (this example sends a HTTP 400 error):

public void checkFoo() {
    if (foo == null || foo.isEmpty()) {
        FacesContext context = Facescontext.getCurrentInstance();
        context.getExternalContext().responseSendError(400, "Foo parameter is required");
        context.responseComplete();
    }
}






如果您碰巧使用JSF实用程序库 OmniFaces ,那么您可以使用 < o:viewParamValidationFailed> 标签用于非常目的,而不需要额外的后台bean逻辑


If you happen to use the JSF utility library OmniFaces, then you can use the <o:viewParamValidationFailed> tag for the very purpose without needing additional backing bean logic.

发送视图参数验证失败的重定向失败:

Sending a redirect on view param validation fail:

<f:metadata>
    <f:viewParam ... required="true">
        <o:viewParamValidationFailed sendRedirect="some.xhtml" />
    </f:viewParam>
</f:metadata>

在视图param验证失败时发送HTTP 400错误:

Sending a HTTP 400 error on view param validation fail:

<f:metadata>
    <f:viewParam ... required="true">
        <o:viewParamValidationFailed sendError="400" />
    </f:viewParam>
</f:metadata>



另请参见:



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