9 Replies Latest reply on Feb 18, 2008 2:36 PM by gavin.king

    Accessing a bean from a bean

    motionfield

      Hello,


      I may have gone about this wrong but, I making a page that gives users the opportunity to download gps-coordinates. It renders with info from a bean (guideSession) after it has been told what to fetch with an id like this:



      <page view-id="/showguide.xhtml">
            <param name="guideId" value="#{guideSession.guideId}"/>
      </page>
      



      the user can then click on a link made like this:



      <h:form>
      <h:commandLink action='#{coordinateDownload.getGarmin}'>Garmin</h:commandLink><br/>
      </h:form>
      



      (sidenote: why do I have to write getGarmin and not just garmin here? I have to omit get everywhere else...)
                     
      the coordinateDownload bean must now know the guideId too. I've tried to access guideSession from coordinateDownload like this to retrieve it:



      GuideSession guideSession = (GuideSession) Contexts.getApplicationContext().get("guideSession");
      



      but it keeps returning null. The debug page tells me that guideSession DOES live in the application context.


      guideSession is just Stateful in default scope.


      coordinateDownload is Stateless in the PAGE scope.


      why can't the guidesession bean be fetched from the ApplicationContext?

        • 1. Re: Accessing a bean from a bean

          sidenote: It is an action not a binding to a value. Bindings to values need get(is)/set. Bindings to actions has the actual method name.


          I don't really understand what the problem is: Are you posting guideId to the showGuide page? Then you get it with RequestParameter annotation.

          • 2. Re: Accessing a bean from a bean
            motionfield
            thanks for your quick reply! ah I see about the get. not very consistent though?

            hmm well I guess I could use RP annotation here. but why isn't it possible to use Contexts.getApplicationContext() this way?

            we'd like to try to stay away from using requestparameters to hold session data and communicate state data between beans.

            say we want to have guideId as a session variable and be able to reach and change it from any bean for this session instead. I read about this in the docs:

            Contexts.getSessionContext().set("guideId", 1);

            Integer guideId = (Integer) Contexts.getSessionContext().get("guideId");

            but where would you define the default value? do you have to use the database and an entity bean?
            • 3. Re: Accessing a bean from a bean
              motionfield

              thanks for your quick reply! ah I see about the get. not very consistent though?


              hmm well I guess I could use RP annotation here. but why isn't it possible to use Contexts.getApplicationContext() this way?


              we'd like to try to stay away from using requestparameters to hold session data and communicate state data between beans.


              say we want to have guideId as a session variable and be able to reach and change it from any bean for this session instead. I read about this in the docs:


              Contexts.getSessionContext().set("guideId", 1);
              
              Integer guideId = (Integer) Contexts.getSessionContext().get("guideId");




              but where would you define the default value? do you have to use the database and an entity bean?

              • 4. Re: Accessing a bean from a bean

                I don't know if there is a best practice on where to store defaults with Seam (if there is, I'd like to now too).


                I (most often) use Seam with spring and simply put default in springs appcontext. You could probably put them in web.xml as a context-parameter as well.


                Between requests isn't is easier to @In(scope=...) @Out(scope=...) data rather than Context.get...Context().get/set(data) ?

                • 5. Re: Accessing a bean from a bean
                  motionfield

                  ok in which docs should I look for more info on web.xml and context-parameters? there doesn't seem to be a whole lot about custom context-params in the seam docs...


                  I've tried In and Out but I'm having trouble understand exactly what can be injected and outjected and where it goes and can be reached.


                  my stateless bean could not just inject the stateful one and access it.. maybe if I made it stateful too?


                  my biggest concern seem to be that I understand the examples and docs pretty well but I don't understand the underlying concepts enough that I know what to do in other cases than the examples present..

                  • 6. Re: Accessing a bean from a bean
                    motionfield

                    btw what does GA and A1 stand for in the version numbers of seam?

                    • 7. Re: Accessing a bean from a bean

                      web.xml is a servlet thingy, not introduced by seam at all.


                      I look upon In and Out as operations that just put/get stuff from some kind of object space. It is hard to describe easy, without going in to specifics... but the seam documentation is quite good. Playing around is probably the easiest way to get the feel for it.


                      GA is probably General Availability, and A1 Alpha 1. (Before beta). Check http://en.wikipedia.org/wiki/Development_stage


                      Cheers

                      • 8. Re: Accessing a bean from a bean
                        gavin.king

                        ah I see about the get. not very consistent though?

                        It is totally consistent. A value binding refers to a get/set pair of a Java-beans-style property, so you would not want to mention the get. A method binding refers to an actual method, which need not follow any special naming convention.


                        This is just the basics of unified EL.

                        • 9. Re: Accessing a bean from a bean
                          gavin.king

                          but why isn't it possible to use Contexts.getApplicationContext() this way?

                          It is possible. If it returns null, it means that there is nothing in the application context with that name.


                          However, it seems to me that if you're calling getApplicationContext() explicitly, you're either doing something extremely sophisticated, or just not using Seam properly.


                          What's wrong with injection? Or Component.getInstance()? Why aren't you keeping this state in a Seam component?


                          Beyond that, I really don't understand what you're trying to do.