在将页面加载到JSF2中之前进行重定向 [英] Redirect before loading the page in JSF2

查看:51
本文介绍了在将页面加载到JSF2中之前进行重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要求,在页面即将加载之前,我想检查查询字符串是否存在,如果查询字符串存在,那么我想重定向到另一个页面而不是当前页面,我该如何处理这种类型的要求JSF 2.

I have a requirement that before the page is going to load i want to check whether query string exists or not if query string exists then i want to redirect to another page instead of current page how can i handle this type of requirement in JSF 2.

预先感谢

推荐答案

在JSF 2.2上,您可以为此使用<f:viewAction>.

When on JSF 2.2, you can use <f:viewAction> for this.

<f:metadata>
    <f:viewParam name="paramName" value="#{bean.paramName}" />
    <f:viewAction action="#{bean.check}" />
</f:metadata>

(paramName是您的查询字符串参数的名称)

(paramName is the name of your query string parameter)

private String paramName; // +getter+setter

public String check() {
    if (paramName == null) {
        return "error.xhtml";
    }

    return null;
}

当尚未使用JSF 2.2(JSF 2.0/2.1)时,您可以为此使用<f:event type="preRenderView">.

When not on JSF 2.2 yet (JSF 2.0/2.1), you can use <f:event type="preRenderView"> for this.

<f:metadata>
    <f:viewParam name="paramName" value="#{bean.paramName}" />
    <f:event type="preRenderView" listener="#{bean.check}" />
</f:metadata>

private String paramName; // +getter+setter

public void check() throws IOException {
    if (paramName == null) {
        FacesContext.getCurrentInstance().getExternalContext().redirect("error.xhtml");
    }
}

另请参见:

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