在 JSF2 中加载页面之前重定向 [英] Redirect before loading the page in JSF2
本文介绍了在 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 上,您可以使用
来解决这个问题.
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");
}
}
另见:
- 什么可以<f:metadata>,<f:viewParam>和 <f:viewAction>用于?
这篇关于在 JSF2 中加载页面之前重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文