3 Replies Latest reply on Feb 4, 2008 7:52 PM by gus888

    How to prevent page data reloading when click Back

    gus888

      Hi there,

      When I run the Seam example (seam-space), I browsed several pages, then I clicked Back to go back the previous browsed pages, I found there are no any page data reloading from database. However, when I clicked Back in my project, every previous page would reload data again from database. Anybody know how to prevent the data reloading? Thank you very much in advance!

        • 1. Re: How to prevent page data reloading when click Back
          blabno

          I guess, that backing bean gets initialized on every request, which may be due to too narrow scope.

          • 2. Re: How to prevent page data reloading when click Back
            arvhatte

            Hi gus888,

            By default your browser shows you a page from its cache when you press the back button in your browser, except when that page has accepted POSTDATA, so was requested with the POST method instead of the GET method. The POST method is (generally) used when submitting form data en the GET method is (generally) used when simply retrieving information. In Seam the POST method is also used to propagate the conversation id (see http://labs.jboss.com/jbossseam/faq/#get, so if you try to go back to a page where you posted data or when in a conversation scope, your browser will try to re-post the data and therefore hit your server again, causing your data to be reloaded.

            You can of course prevent this by making use of 'redirect' in your pages.xml to redirect to the new page after the POST is finished (basically accepting the POST, and then doing a new GET request to the new page). Another approach might be by storing data in a wider scope, which would still make a call to the server (when pressing the back button), but because the data is still present, it won't refetch that data from the database.

            Hope this was helpful, if not, please let me know by replying again. If possible, also include your pages.xml and the two backing beans of the pages you're navigating between by using the back button.

            Greetings,
            Tim

            • 3. Re: How to prevent page data reloading when click Back
              gus888

              Hi Arvhatte, thank for your very detailed explanation. It is really helpful for me to understand the Seam framework.

              However, I still couldn't figure out why I click Back, pages always act with back beans. When I run Seam samples, then click page Back, it seems it run on client, and pages seems don't have any action with server. Following is my partial code snippets. I really appreciate any help. Thanks in advance.

              @Name("profileLister")
              @Statefull
              @Scope(ScopeType.EVENT)
              public class ProfileListerBean implements ProfileLister {
              
               @Out(required=false, scope=ScopeType.PAGE)
               private List<ContactAddress> myContactAddressList;
              
               @Factory("myContactAddressList")
               public String loadContactAddress() {
               myContactAddressList = em.createNamedQuery("contactAddress.*:personId")
               .setParameter("personId", user.getId()).getResultList();
               return "prfl-myContactInfo";
               }
              }
              
              
              pages.xhtml
              <page view-id="*">
               <navigation>
               <rule if-outcome="prfl-myContactInfo">
               <redirect view-id="/profile/my/contact_info.xhtml"/>
               </rule>
               ....