Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSF redirect to other page

I have three XHTML pages;

  1. index.xhtml
  2. page_1.xhtml
  3. 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?

like image 924
yash Avatar asked Feb 27 '12 10:02

yash


People also ask

How to redirect to another page in JSF?

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.

How to redirect URL in JSF?

To enable the page redirection in JSF 2.0, you can append “ faces-redirect=true ” at the end of the outcome string. Page forward. Page redirection. In the navigation rule, you can enable the page redirection by adding a <redirect /> element within the <navigation-case /> .

What is navigation in JSF?

Navigation between different pages of a JavaServer Faces application, such as choosing the next page to be displayed after a button or hyperlink component is clicked, is defined by a set of rules. Navigation rules can be implicit, or they can be explicitly defined in the application configuration resource file.

Which return type of a method in a managed bean is used for page navigation?

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.


2 Answers

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:

  • How to navigate in JSF? How to make URL reflect current page (and not previous one)
  • When should I use h:outputLink instead of h:commandLink?
like image 112
BalusC Avatar answered Oct 21 '22 03:10

BalusC


You can also do this, in any part of your code to be redirected to "example.xhtml"

ExternalContext ec = FacesContext.getCurrentInstance()
        .getExternalContext();
try {
    ec.redirect(ec.getRequestContextPath()
            + "/faces/jsf/example.xhtml");
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
like image 20
juldeh Avatar answered Oct 21 '22 04:10

juldeh