7 Replies Latest reply on Aug 22, 2006 11:22 PM by gavin.king

    When are components created?

    smokingapipe

      I created a very simple component like this:

      @Name("requestBean")
      @Scope(ScopeType.EVENT)
      public class RequestBean {
       private static final Logger logger = Logger.getLogger("RequestBean");
      
       public RequestBean() {
       logger.info("Created a request bean!");
       }
      
       private String affiliateId;
      
       @RequestParameter
       public String getAffiliateId() {
       return affiliateId;
       }
      
       public void setAffiliateId(String affiliateId) {
       logger.info("Setting the affiliate id param: " + affiliateId);
       this.affiliateId = affiliateId;
       }
      
      }
      


      It would seem that, because the scope is EVENT, this bean would need to be instantiated for every single seam request, and yet I never see it get instantiated. Various other beans do get instantiated, and I'm not sure how Seam decides.

      Also, what is the use of the @RequestParameter annotation? I've put in in various beans (stateful, stateless, entity, JavaBeans, etc) and it never gets called. Is there a way to get access to request parameters?

      All I want to do is have it so that if a user shows up at the site, the affiliateId (which is in a GET parameter) gets saved in a cookie. Shouldn't be this hard but I can't figure out how to even access the parameter within Seam, or even get Seam to instantiate my component.

      Any suggestions welcome.


        • 1. Re: When are components created?
          pmuir

          When the component is instantiated it will live in the event scope NOT the component will be instantiated whenever the event scope is accessed. There is no way (AFAIK) of doing this.

          Beans are instantiated when they are accessed (via @In, @Out, accessed from a JSF page etc.)

          • 2. Re: When are components created?
            smokingapipe

            This seems like a step backwards from JSP. Here's the problem I'm facing:

            I have a signup form where users can select their country. There are about 200 countries, and a I have an enum that lists them all, and within that enum class there is also a static member which is a Map<String,String> that maps keys of country codes ("US") to values of country names ("United States"). Pretty basic stuff.

            It seems like I should be able to use that Map as the value of a selectItems component.

            But I cannot find any way to get that Java Map visible within the JSF!

            I have a SFSB that also is the bean that processes the form. I put in a getCountryMap() method in that that always returns this map. But whenever I try to display the page, that doesn't work, because when the page is first displayed, that SFSB has not yet been instantiated, even though it has an EVENT scope.

            So... this is ridiculous. There must be some way to display simple Java objects within JSF. Is there some way I could inject that Map into one of Seam's contexts?

            Hard things, like redisplaying a form, are very easy in Seam, but simple things, like display some dumb Java objects, are difficult or impossible it appears.

            • 3. Re: When are components created?
              smokingapipe

              I figured out a way to do it. I created a new class, called CountrySelect. I gave it a name, and an Application scope, and one method, which is getCountryMap. So that works now. I suppose if a class has APPLICATION scope, only one instance of it is created?

              • 4. Re: When are components created?
                smokingapipe

                And again, I'm back to "error during model data update". I assume that that error is happening because the object is null. And yet the object has a CONVERSATION scope, so it seems to me that it should always exist as long as their is a conversation.

                Is there some way to tell seam, "Please create this object and put it in the given context so I can process this page"?

                It seems totally random to me how Seam decides which objects to create and when.

                By the way, on my SFSB, I do have @In(creatte=true), and that doesn't seem to do any good.

                • 5. Re: When are components created?
                  smokingapipe

                  Ah, I found out the problem.

                  In my entity class, I had a @Roles(...) annotation which gave role names and scopes. I thought that would be enough to use the object. No! A @Name annotation must be present, even if the given name is not used.

                  • 6. Re: When are components created?
                    smokingapipe

                    And now I have my CountrySelect class working, I figured out that instead of returning a Map, it needs to return a List of SelectItems. So I got that done. Then I found out that, because I'm using an enum for my Country, I need to write a Converter for that. Which turned out to be very easy to do, and within a few minutes I had my enum countries working, displaying a select list, etc. So a good select list on the HTML side, and correct Java style on the Java side (using an enum instead of a bunch of strings). And then I set the EnumType.STRING, so it saves it in the DB as a country code string. It's pretty amazing when all this stuff works.

                    • 7. Re: When are components created?
                      gavin.king

                      STATELESS components are created every time they are used, not EVENT components :-)