5 Replies Latest reply on Nov 24, 2006 8:46 AM by Toni Beckman

    Some philosophy on the context name and access to Java Backi

    Toni Beckman Novice

      Hello,

      I'm using an "Entity Bean" as a backing bean to store the values from a form submited via a a post request.

      I have noticed that in order to be able to access the instance of the "Entity Bean" in a "Session Bean" I have to use a syntax like:

      @In(create=true)
      EntityBean entityBean;

      If I instead only use
      @In
      EntityBean entityBean;

      I get an exception, saying that the "In" requires a value, which implies that the instance of the EntityBean has not yet been bound to any one of the 6 seam contexts by a name.

      Otherwise the second "In" should return an instance. But it returns null.

      However, an instance of the EntityBean must exist somewhere else, because the post requests values must be kept somewhere.

      What confuses me is the syntax of the @In(create=true), because the instance of the EntityBean must have been already created before we reach this annotation.

      Shouldn't it be named more appropriately "(bound=true)" or "(bindToDefinedContext=true)" or something which refelcts this relationship more closely?

      Because if it actually were created just when we reach the annotation, then all the values of it would be null or set to the default.

      Any comments on the subject?

        • 1. Re: Some philosophy on the context name and access to Java B
          Gavin King Master

          The post values are kept in the JSF component tree, not in an entity instance.

          • 2. Re: Some philosophy on the context name and access to Java B
            Gavin King Master

            Note that you do *not* need create=true if you have bounds some submitted form field values to entity bean attributes. You only need it if the entity does not already exist.

            • 3. Re: Some philosophy on the context name and access to Java B
              Toni Beckman Novice

              Hi Gavin,

              I'm currently looking into seam and there are a few things, which confuse me a bit.

              For example, how do we initalize "context variables" appearing in .jsp pages, which do not contain any method or value binding expressions, leaving the referenced "context variables" in the .jsp file out of scope.

              I read in the seam manual that there are several ways to tackle this problem. In 3.3 it says that one could use a @Create, @Factory or Page Action.

              I don't see how the first two would solve my problem, because they only come into the picture, when a seam component gets created, which requires a reference to it from the .jsp page, which does not exist in the first place.

              Defining a Page Action is indeed a solution, but I find it cumbersome to create a page action for every .jsp page, which solely displays information and any CRUD applicaton does so plenty.

              Because of that I get the feeling that one should avoid context variables inside of .jsp files and rather use them inside Seam Components. What would be the right stratey?

              I'm basically looking for a pattern, which helps me to display base data information on .jsp pages, which get directly accessed by bookmarked GET requests, without any GET Parameters.



              • 4. Re: Some philosophy on the context name and access to Java B
                Gavin King Master

                 

                how do we initalize "context variables" appearing in .jsp pages, which do not contain any method or value binding expressions


                I don't quite understand what you mean by this. Like a @Factory method or a factory in components.xml?

                I don't see how the first two would solve my problem, because they only come into the picture, when a seam component gets created


                @Create and @Factory are *totally* different to each other. You've misunderstood the Seam docs. @Create is called after a component is created. @Factory is used to auto-initialize the value of a context variable when it is null.

                Because of that I get the feeling that one should avoid context variables inside of .jsp files and rather use them inside Seam Components.


                This is definitely not right.

                I'm basically looking for a pattern, which helps me to display base data information on .jsp pages, which get directly accessed by bookmarked GET requests, without any GET Parameters.


                I still don't understand. You just use "pull-MVC". ie. seam automatically instantiates a component when it is referenced by the JSP. You don't have to do anything.

                • 5. Re: Some philosophy on the context name and access to Java B
                  Toni Beckman Novice

                  Let me give you a short example to put the above said into a meanigfull context:

                  Lets says I have a database table, which stores customers all of which I would like to list row by row in a JSF table, which is contained inside a single JSP page. Each row also contains action links which represent actions that can be performed on a customer, like edit, delelete, display and "add another address".

                  For the JSP page I only need a reference to a DataModel.

                  The reference to the instance of the DataModel should be obtained from a "context variable", which references an "instance" of a class, which implements DataModel and is kept in one of the 5 seam contexts.

                  If I name the jsp page "displayCustomers.jsp" and call it via a GET request, then I run into the fact, that the context variable is not accessible, because no seam component get's referenced in a MVC pull fashion from the page.

                  That's what I meant by saying ".jsp pages, which do not contain any value or method binding".

                  Because no calls to a seam component have been made from the jsp page, the no context variable for the DataModel has not been set.

                  I could use a Page Action, to achievie this so that it calls some method on a seam component that sets the context variable and loads the clients from the database before the .jsp file gets rendered like this:

                  @DataModel @Out
                  @Factory("loadClientsFromDatabase")
                  List clients;

                  But I find that cumbersome, because I have so many pages which list entities the same fashion.

                  And using "pull-MVC" defeats the purpose of the @DataModel, because the idea of using it is not having to use "pull-MVC". I mean if I use something like:

                  <h:dataTable value="#{seamcomponent.loadClientsViaPullMVC}" var="c" rendered="#{seamcomponent.loadClientsViaPullMVC.rowCount>0}">

                  Then I don't need a @DataModel inside "seamcomponent" anymore. I also noticed, that it is not possible to use @DataModelSelection in a seam component, which does not also contain the @DataModel annotation.

                  That I find also strange, because I can think of many use cases in which you would want another seam component to retrieve the selected Customer to perform some actions on him.

                  For example, if I had a "action link" add address, then in the AddAdressAction I would want to place a @DataModelSelection to get the selected Customer from. If I do this currently, then I won't be able to easily retrieve the customer. I have to put "AddAddressAction.add()" method into the Seam Bean, which has the DataModel, where it does not belong and increases component dependancy.

                  I hope in this light my last post makes more sense?!