命中一个bean方法并在GET请求上重定向 [英] Hit a bean method and redirect on a GET request

查看:145
本文介绍了命中一个bean方法并在GET请求上重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在GlassFish上使用JSF 2和PrimeFaces 2.1.

I'm using JSF 2 and PrimeFaces 2.1 on GlassFish.

我有一个页面,该页面旨在让人们在遵循回调URL(例如,作为嵌入电子邮件的链接或作为某些外部身份验证或付款服务的回调URL参数)之后执行操作.就我而言,我需要重设密码.回调URL具有token GET参数,如下所示:

I have a page that is intended to allow people to perform an action after following a callback URL (e.g. as link embedded in email or as callback URL parameter of some external authentication or payment service). In my case I need to reset the password. The callback URL has a token GET parameter like so:

http://example.com/app/resetPasswordForm.jsf?token=abc123

resetPasswordForm.jsf的页面加载中,我需要检查令牌是否有效,并在令牌无效时重定向到主应用程序屏幕.

On page load of resetPasswordForm.jsf, I need to check if the token is valid and redirect to the main app screen if it's not valid.

我的想法是要有一个像这样的bean方法:

My thinking is to have a bean method like:

public String resetPasswordHandler.showResetForm(String token) {
  if /* token is valid */ {
    return "resetPasswordForm.jsf";
  } else {
    return "main.jsf";
  }
}

但是我将如何使该方法在页面加载时被点击?

But how would I cause that method to get hit on page load?

不确定如何进行-欢迎提出建议.

Not sure how to proceed -- suggestions are welcome.

推荐答案

使用

Use <f:viewAction> to trigger a bean method before rendering of the view and simply return a navigation outcome (which will implicitly be treated as a redirect).

例如

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

使用

@ManagedBean
@RequestScoped
public class Authenticator {

    private String token;

    public String check() {
        return isValid(token) ? null : "main.jsf";
    }

    // Getter/setter.
}

如果尚未使用JSF 2.2,则可以使用 解决方法与

If you're not on JSF 2.2 yet, then you can use the <f:event type="preRenderView"> workaround in combination with ExternalContext#redirect().

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

使用

@ManagedBean
@RequestScoped
public class Authenticator {

    private String token;

    public void check() throws IOException {
        if (!isValid(token)) {
            FacesContext.getCurrentInstance().getExternalContext().redirect("main.jsf");
        }
    }

    // Getter/setter.
}

另请参见:

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