9 Replies Latest reply on Sep 18, 2007 1:07 PM by igx89

    Multiple beans outjecting same variable - how to control?

      Let's say I have several beans, all outjecting variable with the same name - is there a way to control which one does the outjection?

        • 1. Re: Multiple beans outjecting same variable - how to control

          AFAIK, outjection is done after each call of each method of a component.

          I don't really see your point.

          • 2. Re: Multiple beans outjecting same variable - how to control
            pmuir

            No.

            • 3. Re: Multiple beans outjecting same variable - how to control

               

              "vincent.latombe" wrote:
              AFAIK, outjection is done after each call of each method of a component.

              Yes, if there's a method call - but what if there isn't one?

              My use case is pretty simple, actually: there's an entity X, with an User attribute, so that each user can see their own Xs, while an admin can see all of them (optionally filtetred by user). Thus, there's an EntityQuery set up ("FROM X x") with restrictions in the form of "x.user = #{selectedUser}", where selectedUser is outjected. Admin screen either outjects null (default on page load) or the user that list should be filtered on (via method call in admin screen controller). User screen, OTOH, is to automatically outject the current logged in user as selectedUser. To accomplish this, the controller bean for user screen declares
              @In @Out ("selectedUser") User principal;
              (principal is the current logged in user, outjected into session on login).

              The only problem is, there's no way to control which of the controller beans would outject the value first (on first page load, for instance). The hacker way to solve this is to introduce a no-op method in each of the controllers, that would execute on page load and do all the outjection, like that:
              <page view-id="/attestation/accounts.xhtml"
               action="#{accountController.noOp}" />
              , but of course this is just a hack...

              • 4. Re: Multiple beans outjecting same variable - how to control

                Have you checked @Factory annotation to do that?

                • 5. Re: Multiple beans outjecting same variable - how to control

                  The whole idea was to use "selectedUser" in a query without having to reference it via any particular controller bean. How would a @Factory help here? It still requires the property to be referenced via beanName.propertyName, unless I'm mistaken...

                  • 6. Re: Multiple beans outjecting same variable - how to control
                    pmuir

                     

                    "ASavitsky" wrote:
                    It still requires the property to be referenced via beanName.propertyName, unless I'm mistaken...


                    You are mistaken, read the docs.

                    • 7. Re: Multiple beans outjecting same variable - how to control

                      A method marked with @Factory("foobar") is called if the foobar variable in context contains null when foobar is referenced in EL. Check Seam tutorial for a working use case.

                      • 8. Re: Multiple beans outjecting same variable - how to control

                        No, still doesn't work - if I add the factory only to the user controller, then admin controller uses it as well, as it's the only one defined (filtering where it shouldn't), and if I add one to admin as well (returning null, of course), then user controller uses that one, too...

                        So I guess I'm back to the no-op method, which seem to be the only working here :( Anyway, thanks for help.

                        I guess it would be nice to be able to specify a default resolver for a page (in pages.xml), so that on page load, a particular bean would handle all outjection - what do you think, is this a sensible way to handle such situation?

                        • 9. Re: Multiple beans outjecting same variable - how to control

                          I'm also running into a similar problem: I have a controller page outjecting an object and then conditionally redirecting to another page (which often starts a long pageflow), and in one of the subsequent pages I need to change which object is pointed to by that initial outjection. Is there a way to set precedence, so I could say that my controller doesn't outject that object if a different component has already outjected something with the same name?

                          My temporary solution is to call a setter on my controller when I want to change the outjected object -- a dirty hack, but at least it works.