0

I want to have a simple paging mechanism so when I call /allhotels?page=2 I want to go back to /allhotels?page=1 or forth to /allhotels?page=3. I have 2 simple buttons:

<h:form>
    <h:commandButton value="previous" action="#{hotelSearchResult.toPreviousPage}" />
    <h:commandButton value="next" action="#{hotelSearchResult.toNextPage}" />
</h:form>

Which should redirect to the next/previous page of a data set:

public int getPreviousPage() {
    return page > 0 ? page - 1 : 0;
}

public void toPreviousPage() {
    int previous = getPreviousPage();
    ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
    String url = context.getRequestContextPath() + "/allhotels?page=" + previous;
    try {
        context.redirect(url);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public int getNextPage() {
    return hotels != null && hotels.size() == 100 ? page + 1 : page;
}

public void toNextPage() {
    int next = getNextPage();
    ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
    String url = context.getRequestContextPath() + "/allhotels?page=" + next;
    try {
        context.redirect(url);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

but it only calls /pages/allhotels.xhtml?

I have tried so many things but it never worked:

<f:viewParam name="page" value="#{hotelSearchResult.page}" />
<h:commandButton value="vor" action="#{hotelSearchResult.toNextPage}">
    <f:param name="page" value="#{hotelSearchResult.nextPage}" />
</h:commandButton>

faces.config.xml:

<navigation-rule>
    <from-view-id>/allhotels</from-view-id>
    <navigation-case>
        <from-outcome>*</from-outcome>
        <to-view-id>*</to-view-id>
        <redirect>
            <redirect-param>
                <name>page</name>
                <value>*</value>
            </redirect-param>
        </redirect>
    </navigation-case>
</navigation-rule>

The 2 methods To...Page() dont even get invoked ... can somebody tell me how I can get the simplest paging mechanism possible? I already lost 2 hours ...

Pali
  • 1,337
  • 1
  • 14
  • 40

1 Answers1

0

There are many almost duplicates here regarding sending parameters and redirecting. I don't get why you are over complicating things by checking if you can go back or forward in your bean. This will mean that on page 1 you have a back link which redirects you back to page 1? I would keep things simple and use the disabled attribute on a h:link. So:

<h:link value="previous" disabled="#{hotelSearchResult.page le 1}">
    <f:param name="page" value="#{hotelSearchResult.page - 1}" />
</h:link>
<h:link value="next" disabled="#{hotelSearchResult.page ge 100}">
    <f:param name="page" value="#{hotelSearchResult.page + 1}" />
</h:link>

Regarding processing the parameter, see:

Community
  • 1
  • 1
Jasper de Vries
  • 19,370
  • 6
  • 64
  • 102