I have three XHTML pages;
- index.xhtml
- page_1.xhtml
- page_2.xhtml
In the index.xhtml
page, I have a commandButton
which sends the user to page_1.xhtml
. All this is done in the navigation rule in faces-config.xml
.
How would I redirect the user to page_2.xhtml
from the index.xhtml
using another commandButton
assuming that both commandButtons
' actions are linked to a backing Java class?
You can also do this, in any part of your code to be redirected to "example.xhtml"
Add two navigation cases as shown below. In the action methods, return outcomes corresponding to the buttons.
Just bind the buttons to different action methods which each return a different outcome.
with
Navigation cases are not necessary. JSF 2.0 supports implicit navigation. The navigation outcome can just be the path/filename of the desired target view. The file extension in the outcome is optional.
If you don't necessarily need to perform any business action on navigation, or you can do it in the (post)constructor of the backing bean of the target page instead, then you can also just put the outcome value in the
action
directly.A
<h:commandButton>
will however not perform a redirect, but a forward. The enduser won't see the URL being changed in the browser address bar. The target page isn't bookmarkable. If you can, I'd suggest to use<h:button>
instead.Or if you really need to invoke a business action, but would like to perform a real redirect, then append
faces-redirect=true
as query string to the outcome value.See also: