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

Jsf 如何在托管bean中导航到另一个页面?,jsf,redirect,navigation,forward,Jsf,Redirect,Navigation,Forward,我正在尝试使用commandbutton转发托管bean中的页面: <h:commandButton action="#{bean.action}" value="Go to another page" /> 重定向页面,而不是转发。我看到了与此类似的问题,并尝试了给定的解决方案: public void action() throws IOException { FacesContext.getCurrentInstance().getExternalContext().

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

<h:commandButton action="#{bean.action}" value="Go to another page" />
重定向页面,而不是转发。我看到了与此类似的问题,并尝试了给定的解决方案:

public void action() throws IOException {
    FacesContext.getCurrentInstance().getExternalContext().dispatch("another.xhtml");
}
但我得到了以下错误:

Index: 0, Size: 0

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

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

public String action() {
    return "another.xhtml";
}
如果您除了导航之外没有做任何其他事情,那么您也可以直接将字符串结果放入
action
属性中

<h:commandButton action="another.xhtml" value="Go to another page" />

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

<h:button outcome="another.xhtml" value="Go to another page" />

另见:

你能发布异常堆栈跟踪吗?@erencan我想我已经解决了,谢谢你谢谢,我想我不知道button和commandbutton之间的区别。还有一个问题,让我们假设对于两个页面,我使用相同的托管bean,在第1页我有一个文本框,我填充它并按下按钮(不是命令按钮),setter和getter工作,一些变量绑定到该框的值。然后页面1被导航到文本2。您说过simple按钮不执行post请求,所以在第2页中,我可以得到绑定到第1页文本框值的变量的值吗?还是必须将其作为参数传递到第2页?取决于具体的功能需求。为什么需要在不同的视图中显示POST请求的结果?您不能通过例如
rendered=“#{notempty bean.results}”
在同一视图中有条件地显示结果吗?另请参见第二个“另请参见”链接以获取建议。
<h:button outcome="another.xhtml" value="Go to another page" />