JSF redirect to other page
Asked Answered
D

3

11

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?

Diophantus answered 27/2, 2012 at 10:15 Comment(6)
why don't you duplicate and change the navigation rules and command buttons ?Spoondrift
@Spoondrift it is not working like this tooDiophantus
Are you using JSF 1.x or 2.x? Do you really need to invoke a managed bean action method on page-to-page navigation?Mewl
@Mewl its JSF 2.0.. yes i think so because i have to display different values on each pageDiophantus
Why do you want to use navigation rules? Are you sure that you aren't focusing too much on JSF 1.x tutorials? Also, do those requests really have to be POST requests rather than GET requests?Mewl
@Mewl Can you tell me how to do it without using navigation rules?Diophantus
M
30

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:

Mewl answered 27/2, 2012 at 13:13 Comment(0)
L
6

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();
}
Lighterman answered 10/9, 2015 at 7:27 Comment(0)
B
2

Add two navigation cases as shown below. In the action methods, return outcomes corresponding to the buttons.

            <navigation-rule>
                <from-view-id>index.html</from-view-id>
                <navigation-case>
                    <from-outcome>page1</from-outcome>
                    <to-view-id>page_1.xhtml</to-view-id>
                </navigation-case>
                <navigation-case>
                    <from-outcome>page2</from-outcome>
                    <to-view-id>page_2.xhtml</to-view-id>
                </navigation-case>
            </navigation-rule>
Bunni answered 27/2, 2012 at 10:27 Comment(3)
how would you add re-direct in this scenarioAbsenteeism
I have the same issue with ?faces-redirect=true being added to the backing bean's return "some_view?faces-redirect=true"; it gives me the missing navigation rule message.Patroclus
@AjaySharma within a <navigation-case> block you can add the self closing tag <redirect />Aqueduct

© 2022 - 2024 — McMap. All rights reserved.