5 Replies Latest reply on Oct 12, 2005 1:13 PM by Jacob Hookom

    loading data on initial display of view

    Tom Goring Apprentice

      What is the preferred way to load data (say a List of Hotels) on the initial render of a screen. All the examples I have seen in the booking demo do this stuff on action events.

      e.g. should I implement Lifecycle beginInitialization or something to load the statefull session bean with the data ? Does not seem the correct way to go.

        • 1. Re: loading data on initial display of view
          Norman Richards Master

          This is something that I've been struggling with. The way I did this pre-seam was to simply have a getData() method on the backing bean that fetches the data and returns it immediately. (use #{bean.data})

          The problem I always ran into was that I might first want to test #{empty(bean.data)} somewhere. This would cause multiple database reads. So then, I had to add code to make sure I only read the data once. It gets messy, but you can obviously do the same thing in seem. (just make sure you always intercept if you are making use of data injected by seam)

          Another problem with that approach is that you are loading data during the render view phase, which is generally a bad idea.

          When doing my apps, I've often wished I could have some sort of implicit action be invoked. If a request for view "foo" came in with no action, I might like "#{fooBean.doSomething}" to be invoked. That would give me enough to solve most of my problems.

          • 2. Re: loading data on initial display of view
            ryan dewell Novice

            Perhaps you could use the @Create lifecycle event on the bean that manages the hotel data? Seam will call this method and allow you to populate/configure the bean however you want.

            Ryan

            • 3. Re: loading data on initial display of view
              Norman Richards Master

              Actually, @Create works incredibly well for solving the multiple reads problem. Assuming you can live with data access during render response, there's one other big problem. You can't start a conversation during render response. (Even if you @Begin next to your @Create, it's just too late) That means that you couldn't click through on any of the data you preloaded, at least not in the context of a conversation. You'd still need an action link to start the conversation, which defeats the purpose.

              If I hack Seam to make it think that every conversation should be saved, (a small change to Manager.store()) then everything works marvelously. The downside that seam will store every single conversational context until the conversation timeout expires. That's probably too much overhead, but perhaps this could be the default only for certain pages. Could a tag on the view template insert the conversation id? If not, maybe we need a better conversation timeout strategy? Maybe implicit conversations are stored for a short amount of time (unless they are accessed again) and explicit conversations (those saved with @Begin) are saved for a long time?

              I don't really know the implications, but I know we absolutely need to be able to directly hit a view page that shows data that you can act on conversationally.


              • 4. Re: loading data on initial display of view
                ryan dewell Novice

                Another solution that I use for this is as follows:

                - Create a custom PhaseListener for after the restore view phase (could be another phase perhaps?)
                - If it is a GET request, then inspect the URL.
                - Depending on the URL, load a component by name using Component.getInstance.
                - Call a method on the component. For example: ProductDetailBean.view()
                - Access the NavigationHandler from the FacesContext.
                - Pass: "#{ProductDetailBean.view}" and the outcome of the previous method call to the NavigationHandler.
                - JSF then handles navigation normally, and the normal Seam annotations for conversations are honored.

                In short, you transform GET requests into method calls.

                Ryan

                • 5. Re: loading data on initial display of view
                  Jacob Hookom Newbie

                  Nice work Ryan, I really wish Seam had a wiki where you could share your ideas if you wanted.