如何导航到托管bean中的另一个页面?

yra*_*lik 10 navigation jsf redirect forward

我正在尝试使用commandbutton在我的托管bean中转发页面:

<h:commandButton action="#{bean.action}" value="Go to another page" />
Run Code Online (Sandbox Code Playgroud)

以下行:

public void action() throws IOException {
    FacesContext.getCurrentInstance().getExternalContext().redirect("another.xhtml");
}
Run Code Online (Sandbox Code Playgroud)

重定向页面,而不是转发.我已经看到了类似的问题并尝试了给定的解决方案:

public void action() throws IOException {
    FacesContext.getCurrentInstance().getExternalContext().dispatch("another.xhtml");
}
Run Code Online (Sandbox Code Playgroud)

但是我收到以下错误:

Index: 0, Size: 0
Run Code Online (Sandbox Code Playgroud)

那么如何从托管bean转发到页面?

Bal*_*usC 10

只需将其作为操作方法返回值返回.

public String action() {
    return "another.xhtml";
}
Run Code Online (Sandbox Code Playgroud)

如果你反过来没有做任何其他事情而不是导航,那么你也可以直接将字符串结果放在action属性中.

<h:commandButton action="another.xhtml" value="Go to another page" />
Run Code Online (Sandbox Code Playgroud)

然而,这反过来又是一种相当糟糕的做法.您不应该执行纯页面到页面导航的POST请求.只需使用简单的按钮或链接:

<h:button outcome="another.xhtml" value="Go to another page" />
Run Code Online (Sandbox Code Playgroud)

也可以看看: