层次结构中上层文件的路径

Path to a file up in the hierarchy

这是我的 JSF 应用程序的文件结构。

User 目录是安全的,需要进行身份验证才能查看 user/success.xhtmluser/success.xhtml 有一个用于注销的按钮。该按钮将表单提交给以下方法。

public String logout(){
        HttpSession session = (HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession(false);
        session.invalidate();
        return "index";
    }

上述方法的最后一行是将用户重定向到 index.xhtml,因为 index.xhtmluser/success.xhtml 不在同一目录中,所以我收到以下错误。

Unable to find matching navigation case with from-view-id '/user/success.xhtml' for action '#{AccContrl.logout()}' with outcome 'index'

如何重定向到层次结构中出现的文件? 我试过 return "/../index"; 但没用。

导航结果表示视图 ID。如果它不是以 / 开头,那么它被解释为相对于当前路径。如果它以 / 开头,则它是相对于 Web 根目录进行解释的。

因此,只需使用 /index

return "/index";

与具体问题无关,你应该在这里重定向,而不是导航。不仅因为当你打算在 POST 之后离开时你应该总是执行重定向,而且因为无效的会话仍然存在于当前 request/response 中,但只是不再存在于下一个会话中。

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

另请参阅:

  • What is the difference between redirect and navigation/forward and when to use what?
  • How to navigate in JSF? How to make URL reflect current page (and not previous one)
  • How to invalidate session in JSF 2.0?