Just bind the buttons to different action methods which each return a different outcome.
<h:commandButton value="Go to page 1" action="#{bean.goToPage1}" />
<h:commandButton value="Go to page 2" action="#{bean.goToPage2}" />
with
public String goToPage1() {
// ...
return "page_1";
}
public String goToPage2() {
// ...
return "page_2";
}
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.
<h:commandButton value="Go to page 1" action="page_1" />
<h:commandButton value="Go to page 2" action="page_2" />
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.
<h:button value="Go to page 1" outcome="page_1" />
<h:button value="Go to page 2" outcome="page_2" />
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.
public String goToPage1() {
// ...
return "page_1?faces-redirect=true";
}
public String goToPage2() {
// ...
return "page_2?faces-redirect=true";
}
See also:
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…