本文介绍了如何导航到托管bean中的另一个页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用命令按钮在托管bean中转发页面:

I am trying to forward a page in my managed bean with the commandbutton:

<h:commandButton action="#{bean.action}" value="Go to another page" />

以下行:

public void action() throws IOException {
    FacesContext.getCurrentInstance().getExternalContext().redirect("another.xhtml");
}

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

redirects the page, not forwards. I have seen a similar question to this and tried the given solution:

public void action() throws IOException {
    FacesContext.getCurrentInstance().getExternalContext().dispatch("another.xhtml");
}

但是出现以下错误:

Index: 0, Size: 0

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

So how can I forward to a page from a managed bean?

推荐答案

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

Just return it as action method return value.

public String action() {
    return "another.xhtml";
}

如果您除了导航之外不执行其他任何操作,那么您也可以将字符串结果直接放在action属性中.

If you're in turn not doing anything else than navigating, then you could also just put the string outcome directly in action attribute.

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

但是,这反过来又是一个很差的做法.您不应该为纯页面到页面的导航执行POST请求.只需使用简单的按钮或链接:

However, this is in turn a rather poor practice. You should not be performing POST requests for plain page-to-page navigation. Just use a simple button or link:

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

另请参见:

09-27 16:15