【问题标题】:Navigation to pages in subfolders in JSF 2 does not work导航到 JSF 2 中子文件夹中的页面不起作用
【发布时间】:2015-11-09 14:31:38
【问题描述】:

我遇到以下问题:我有一个文件夹层次结构,用于分隔我的项目网页:

Web
 |- Acoes
 |   |- usuariosAcoes.xhtml
 |   |- cadastrarAcao.xhtml
 |- usuarios
 |   |- listarUsuarios.xhtml
 |   |- cadastrarUsuario.xhtml

但是当我尝试导航 ManagedBean 时,页面不会加载,只有在我尝试访问当前页面文件夹之外的页面时才会更新当前页面。

例如,如果我在 listarUsuarios.xhtml 页面中并尝试通过 ManagedBean cadastrarUsuario.xhtml 访问该页面,一切正常:

public String acessaCadastro(){ return "cadastrarUsuario"; }

但是,如果我在页面列表 Usuarios.xhtml 上并尝试访问另一个文件夹中的 usuariosAcoes.xhtml,则什么也不会发生,只会重新加载我已经在的页面:

public String acessarAcoesUsuario(){ return "usuariosAcoes"; }

我试过了,但没有解决:

public String acessarAcoesUsuario(){ return "Acoes/usuariosAcoes"; }

所以不要:

public String acessarAcoesUsuario(){ return "../Acoes/usuariosAcoes"; }

即便如此:

public String acessarAcoesUsuario(){ return "Acoes/usuariosAcoes.xhtml"; }

或:

public String acessarAcoesUsuario(){ return "../Acoes/usuariosAcoes.xhtml"; }

那么我该如何解决这个问题呢?记住不要使用 faces-config.xml 创建路由,因为 JSF 2.x 已经抽象了它。

【问题讨论】:

  • 改用“上下文路径”。 (顺便说一下,从纯粹的技术角度来看,faces-config.xml 中的导航规则并没有过时)。

标签: java jsf jsf-2.2


【解决方案1】:

如果我理解的话,我在 4 天前遇到了同样的问题。

当您在按钮中使用操作导航到另一个页面时,该按钮仅在当前页面中呈现目标页面。

我使用这个解决方案:

public void navigateTo(String href) {
        try {
        FacesContext.getCurrentInstance().getExternalContext().redirect("MYSERVERADDRESS" + href);
    } catch (IOException ex) {
    }
}

在你的action中,你可以使用"navigateTo("page.xhtml")" insted返回地址到Button的Action。

public String acessarAcoesUsuario(){
    navigateTo("/Acoes/usuariosAcoes.xhtml");
    return "";
}

或者干脆

public void acessarAcoesUsuario(){
    navigateTo("/Acoes/usuariosAcoes.xhtml");
}

【讨论】:

    猜你喜欢
    • 2011-02-16
    • 2023-03-12
    • 1970-01-01
    • 2021-12-18
    • 1970-01-01
    • 2016-07-20
    • 2015-07-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多