How can I navigate to another page in managed bean?

泄露秘密 提交于 2019-12-08 15:51:41

问题


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

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

The following line:

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");
}

But I get the following error:

Index: 0, Size: 0

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


回答1:


Just return it as action method return value.

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

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" />

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" />

See also:

  • Difference between h:button and h:commandButton
  • 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)
  • ExternalContext#dispatch() doesn't work
  • JSF implicit vs. explicit navigation


来源:https://stackoverflow.com/questions/17631949/how-can-i-navigate-to-another-page-in-managed-bean

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!