Pavel
Pavel

Reputation: 1288

Going back to a page I lose the state

I have a page with results form a search arranged in pages. When I navigate forwards to the detail view and backwards to the result view, the result view is going to page1. How can I fix this? I am using two ViewScoped beans. I tried SessionScoped, but it will do the same. What is the best way to do this?

result page

<f:metadata>
        <f:viewParam name="lang" value="#{search.language}" />
        <f:viewAction action="#{result.init()}" />
</f:metadata>
<h:form>
            <ui:repeat value="#{result.recipesView}" var="rec">
                <h:link value="#{rec.title}" outcome="recipeshow">
                    <f:param name="id" value="#{rec.id}" />
                </h:link>
                <br/>
                <h:outputText value="#{rec.id}"/><br/>
                <h:outputText value="#{rec.author}"/><br/>
                <h:outputText value="#{rec.createDate}"/><br/>
                <br/>
            </ui:repeat>
            <br/>
            <ui:repeat value="#{result.pagesArray}" var="page">
                <h:commandLink value="#{page.pageNumber}" disabled="#{page.pageDisabled}">
                    <f:ajax listener="#{result.doPages()}" render="@form"/>
                    <f:param name="currentPage" value="#{page.pageNumber}"/>
                </h:commandLink>                  &nbsp;
            </ui:repeat>
</h:form>

Upvotes: 0

Views: 220

Answers (1)

skuntsel
skuntsel

Reputation: 11742

If you do the manipulation of view scoped data, like managing the current page via <h:commandLink> it will be available as long as you interact with the current view by making postbacks. When you show the details view you are no longer dealing with results view anymore, so your view information is basically gone. So when you press the browser's back button you will either revert to the first page (in case page is not cached) or to the view as it was left beforehand (in case page is cached), but you'll get a ViewExpiredException.

What you need to do to overcome that difficulty is to hold the information in the URL the back button points to. In other words, give up using post links (<h:commandLink>) to page the results and switch to using get links (<h:link>) instead. The latter will be used to send a new get request holding the relevant information (current page, paging size, paging order, etc.) to show the results. This can be done by using <f:param> and <f:viewParam> tags. In this light when back button is pressed you will be shown the results with the parameters defined in the request. Idempotence of the result is the key in your situation.

So, you'll have a bunch of <f:viewParam> tags to keep the paging data in results view. You also need to change your command links to plain <h:link>s with nested <f:param>s that represent paging data as well.

Upvotes: 2

Related Questions