命中一个bean方法并在GET请求上重定向 [英] Hit a bean method and redirect on a GET request
问题描述
我在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.
}
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.
}
另请参见:
- JSF 2.0中的通信-处理GET请求参数
- >什么可以< f:metadata> ;,< f: viewParam>和< f:viewAction>用于吗?
-
See also:
- Communication in JSF 2.0 - Processing GET request parameters
- What can <f:metadata>, <f:viewParam> and <f:viewAction> be used for?
- How do I process GET query string URL parameters in backing bean on page load?
这篇关于命中一个bean方法并在GET请求上重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!