2

我终于得到了页面之间传递的消息,但这并没有将我重定向到用户登录页面(../../index.xhtml),而是显示了禁止页面:

public String permission() throws IOException {
    FacesContext context = FacesContext.getCurrentInstance();
    Map<String, Object> sessionMap = context.getExternalContext().getSessionMap();
    String isLog = (String) sessionMap.get("isLogged");

    if(isLog != "TRUE") {

        System.out.println("*** The user has no permission to visit this page. *** ");
        context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Info : ", "Loggez-vous"));
        context.getExternalContext().getFlash().setKeepMessages(true);
        //context.getExternalContext().redirect("../../index.xhtml");
        return "../../index.xhtml?faces-redirect=true";


    } else {
        System.out.println("*** The session is still active. User is logged in. *** ");
    }
    return "../../index.xhtml?faces-redirect=true";
}

当然,受限页面有这个:

<f:event type="preRenderView" listener="#{userloginMB.permission()}"/>

使用 get external context 重定向会使消息丢失。

4

2 回答 2

4

忽略一般设计问题(在此处查找起点),您似乎混淆了新的 JSF 2.2<f:viewAction>和旧的 JSF 2.0/2.1<f:event type="preRenderView">技巧。

String仅支持在 GET 中返回导航结果<f:viewAction>,而不在<f:event type="preRenderView">. 对于后者,您需要ExternalContext#redirect()您碰巧评论过。

所以,你应该做

<f:event type="preRenderView" listener="#{bean.onload}"/>
public void onload() throws IOException {
    // ...
    ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
    ec.redirect(ec.getRequestContextPath() + "/index.xhtml");
}

或者

<f:metadata>
    <f:viewAction action="#{bean.onload}"/>
</f:metadata>
public String onload() {
    // ...
    return "/index.xhtml"; // Note: no need for faces-redirect=true!
}

不要混淆它们。

请注意,我以这样一种方式编写代码,即您始终可以使用/path相对于 Web 根目录的相对关系,而无需摆弄../废话。

也可以看看:

于 2015-09-12T23:02:56.407 回答
0

使用faces-redirect=true时,需要从根上下文指定绝对路径。

所以你的结果字符串应该是这样的:

return "/dir1/dir2/index.xhtml?faces-redirect=true";

如果 index.xhtml 位于(上下文根)即Web Content /index.xhtml 中,则使用此结果字符串:

return "/index.xhtml?faces-redirect=true";

如果 index.xhtml 位于Web Content /pages/dir2/index.xhtml 中,则使用以下结果字符串:

return "/pages/dir2/index.xhtml?faces-redirect=true";
于 2015-09-12T22:29:38.700 回答