PrettyFaces重定向不适用于preRenderView事件

时间:2012-02-28 09:50:09

标签: java-ee jsf-2 prettyfaces

我的bean中有一个preRender视图事件,我在用户上进行了一些验证,当出现某些情况时,我使用prettyFaces将用户重定向到登录页面,但重定向似乎不起作用,我不知道为什么,这是代码:

JSF:

<f:event type="preRenderView" listener="#{myBean.preRender}" />

Managed Bean:

public String preRender() {
        log.debug("preRender myPage for user " + userId);
        try {
            User user = userService.getUserById(userId);
            if (!user.isSomeCondition()) {
                log.debug("Bad Condition");
                return "pretty:login";
            }
        } catch (Exception e) {
            log.error("Error in preRender myPage for user "
                    + userId);
            return "pretty:login";
        }

        return null;
    }

1 个答案:

答案 0 :(得分:6)

您无法通过在动作侦听器方法中返回字符串来进行导航。它将被完全忽略。它只能在<h:commandXxx action="...">提供的实际操作方法中使用。

您可以做的是手动调用NavigationHandler#handleNavigation()

FacesContext context = FacesContext.getCurrentInstance();
NavigationHandler navigator = context.getApplication().getNavigationHandler();
navigator.handleNavigation(context, null, "pretty:login");