本文介绍了JSF 中的条件重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有内置机制可以有条件地重定向到另一个视图?如果他/她已经登录,我希望用户从登录页面重定向到主页".
is there a build-in mechanism to conditionally redirect to another view? I want the user to be redirected from the login page to the "home page" if he/she is already logged in.
我已经有两种基本方法,但对于第一种我不知道如何实现,第二种是一种肮脏的解决方法.
I already have two basic approaches, but for the first I have no idea how to achieve and the second is sort of a dirty workaround.
- 添加
<meta http-equiv="Refresh" content="0; URL=home.jsf"/>
并让它有条件地呈现(EL :#{login.loggedIn}
) - 添加一个
<h:panelGroup/>
,它也将有条件地呈现,包含一些执行重定向的 JavaScript.
- Add
<meta http-equiv="Refresh" content="0; URL=home.jsf" />
and let it be rendered conditionally (EL :#{login.loggedIn}
) - Add a
<h:panelGroup />
which will also be conditionally rendered, containing some JavaScript doing the redirect.
有没有办法实现 1 甚至是另一种更优雅的解决方案?:-)
Is there a way to achieve 1 or even another, more elegant solution? :-)
谢谢
推荐答案
You could use <f:event type="preRenderView">
for this.
例如
<f:event type="preRenderView" listener="#{login.checkAlreadyLoggedin}" />
与
public void checkAlreadyLoggedin() throws IOException {
if (isLoggedIn()) {
ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
ec.redirect(ec.getRequestContextPath() + "/home.xhtml");
}
}
这篇关于JSF 中的条件重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!