PrettyFaces重定向不适用于preRenderView事件

fre*_*dev 1 java-ee jsf-2 prettyfaces

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

JSF:

<f:event type="preRenderView" listener="#{myBean.preRender}" />
Run Code Online (Sandbox Code Playgroud)

托管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;
    }
Run Code Online (Sandbox Code Playgroud)

Bal*_*usC 6

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

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

FacesContext context = FacesContext.getCurrentInstance();
NavigationHandler navigator = context.getApplication().getNavigationHandler();
navigator.handleNavigation(context, null, "pretty:login");
Run Code Online (Sandbox Code Playgroud)