JSF中的条件重定向 [英] Conditional redirection in JSF

查看:160
本文介绍了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.

  1. 添加<meta http-equiv="Refresh" content="0; URL=home.jsf" />并使其有条件地呈现(EL:#{login.loggedIn})
  2. 添加一个<h:panelGroup />,它也会有条件地呈现,其中包含一些执行重定向的JavaScript.
  1. Add <meta http-equiv="Refresh" content="0; URL=home.jsf" /> and let it be rendered conditionally (EL : #{login.loggedIn})
  2. 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中的条件重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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