3 Replies Latest reply on May 26, 2009 8:53 PM by brandonsimpson

    what is the preferred way to load smth before the jsf page starts loading

    atamur

      Hi


      I was reading through seam examples and was trying to build smth myself. But I don't quite understand. The examples have 2 blog applications which use different approaches to loading data.


      I would like to associate a particular request to the page blog.seam?blogId=1 with a statefull session bean BlogAction:



      @Stateful
      @Name("blogAction")
      @Scope(EVENT)
      public class BlogAction implements BlogActionLocal {
          private static final int ONE_PAGE_LIMIT = 5;
      
          @In
          private EntityManager entityManager;
      
          @Out
          private Blog blog;
      
          public void loadBlog(Long blogId) {
              blog = entityManager.find(Blog.class, blogId);
          }
      
          @SuppressWarnings({"unchecked"})
          public List<BlogEntry> getEntries() {
              return entityManager
                      .createQuery("select be from BlogEntry be " +
                              "where be.blog.id = #{blog.id} " +
                              "order by be.postDate desc")
                      .setMaxResults(ONE_PAGE_LIMIT)
                      .getResultList();
          }
      
          @Remove
          public void remove() {
          }
      }
      



      my page is going to call blogAction.entries to list the entries, but before that I need somehow to call the loadBlog() method ... I tried using the approach from blog example with


      <?xml version="1.0" encoding="UTF-8"?>
      <page xmlns="http://jboss.com/products/seam/pages"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://jboss.com/products/seam/pages http://jboss.com/products/seam/pages-2.1.xsd">
      
          <param name="blogId" value="#{blog.id}" required="true" />
          
          <action execute="#{blogAction.loadBlog(blog.id)}" />
      
      </page>
      



      but action call hapens later in the jsf live cycle than call to getEntries made by ui:repear


      What is the best practice to organizing REST style web pages like this one?

        • 1. Re: what is the preferred way to load smth before the jsf page starts loading
          atamur

          so essentially my qn is: is it possible to write a backing bean for a page which will be invoked right after requestm before jsf kicks in. I don't like the Factory approach cuz it requires creation of new unique names for each page :(

          • 2. Re: what is the preferred way to load smth before the jsf page starts loading
            pramod_bs

            Do you have a solution for this?

            • 3. Re: what is the preferred way to load smth before the jsf page starts loading
              brandonsimpson
              I don't think that what you think is happening is really happening. I use page actions and to my knowledge they do execute prior to the page being rendered. What is the default scope for blog?

              It looks to me like the page param would be creating a blog object directly then setting the id from the blogId parameter. So depending on the scope of that blog instance, maybe that is causing the wierd results. In other words, could it be that your <param> creates a blog object first and then goes out of scope before your <action> executes? My guess is if you set the blogId directly on your BlogAction, you will get the correct result. For example, do

              <param name="blogId" value="#{blogAction.blogId}" required="true" />

              <action execute="#{blogAction.loadBlog()}" />


              then your blogAction could have something like:

                  private Long blogId;

                  @Out
                  private Blog blog;

                  public void setBlogId(Long blogId)
                  {
                    this.blogId = blogId;
                  }

                  public void loadBlog() {
                      blog = entityManager.find(Blog.class, blogId);
                  }

                  @SuppressWarnings({"unchecked"})
                  public List<BlogEntry> getEntries() {
                      return entityManager
                              .createQuery("select be from BlogEntry be " +
                                      "where be.blog.id = " + blogId +
                                      "order by be.postDate desc")
                              .setMaxResults(ONE_PAGE_LIMIT)
                              .getResultList();
                  }