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?
JSF by default performs a server page forward while navigating to another page and the URL of the application does not change. To enable the page redirection, append faces-redirect=true at the end of the view name. Here, when Page1 button under Forward is clicked, you will get the following result.
Just return it as action method return value. If you're in turn not doing anything else than navigating, then you could also just put the string outcome directly in action attribute.
You can specify one of the following scopes for a bean class: Application (@ApplicationScoped): Application scope persists across all users' interactions with a web application. Session (@SessionScoped): Session scope persists across multiple HTTP requests in a web application.
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" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With